У меня есть страница расширенного поиска с отдельным контроллером поиска для моих контрактов.
Мой поиск работает нормально, но ищет ВСЕ записи, я только хочу, чтобы он осуществлял поиск записей, связанных с текущим пользователем.
Я использую Devise.
Ниже мой search.rb
class Search < ActiveRecord::Base
def search_contracts
contracts = Contract.all
contracts = contracts.where(user_id: current_user)
contracts = contracts.joins(:tenant).where(["tenants.first_name LIKE ? OR tenants.last_name LIKE ?", "%#{keywords}%", "%#{keywords}%"]) if keywords.present?
contracts = contracts.where(["balance >= ?", min_balance]) if min_balance.present?
contracts = contracts.where(["balance <= ?", max_balance]) if max_balance.present?
contracts = contracts.where(["unpaid_rent LIKE ?", unpaid_rent]) if unpaid_rent.present?
return contracts
end
end
Вот мой search_controller.rb
class SearchesController < ApplicationController
def new
@search = Search.new
end
def create
@search = Search.create!(search_params)
redirect_to @search
end
def show
@search = Search.find(params[:id])
end
private
def search_params
params.require(:search).permit(:keywords, :min_balance, :max_balance, :unpaid_rent)
end
end
показать страницу результатов поиска
.wrapper_with_padding
#houses.clearfix
- unless @search.user_search_contracts.blank?
- @search.user_search_contracts.each do |contract|
%a{ href: (url_for [contract])}
.house
%p.end_of_contract= contract.end_of_contract
%p.balance= number_to_currency(contract.balance, :unit => "£", negative_format: "(%u%n)")
-if contract.tenant.present?
%p.tenant_id= contract.tenant.full_name
-else
%p No Tenant Assigned
-if contract.house.present?
%p.house_id= contract.house.full_house_name
-else
%p No Property Assigned
- else
%h2 Add a Contract
%p It appears you have not added any contracts
%button= link_to "New Contract", new_contract_path
= link_to "Advanced Search", new_search_path