Sql 仅在ruby on rails上搜索当前用户记录

Sql 仅在ruby on rails上搜索当前用户记录,sql,ruby-on-rails,ruby,devise,Sql,Ruby On Rails,Ruby,Devise,我有一个高级搜索页面,有一个单独的合同搜索控制器 我的搜索工作正常,但正在搜索所有记录,我只希望它搜索与当前用户关联的记录 我正在使用这个装置 下面是我的search.rb class Search < ActiveRecord::Base def search_contracts contracts = Contract.all contracts = contracts.where(user_id: current_user) contracts

我有一个高级搜索页面,有一个单独的合同搜索控制器

我的搜索工作正常,但正在搜索所有记录,我只希望它搜索与当前用户关联的记录

我正在使用这个装置

下面是我的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

您可以对搜索模型执行类方法

class Search < ActiveRecord::Base
  def self.user_search_contracts(user)
    user ||= User.new
    contracts = Contract.where(user_id: user.id)

    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
类搜索=?”,最小余额]?

contracts=contracts.where([“balance虽然,我不会以这种方式实现这个搜索功能,但是

尝试修改@DR7发布的内容,只创建实例方法,而不是类方法

类搜索=?”,最小余额]?

contracts=contracts.where([“余额向搜索模型添加初始化方法并将其发送给当前用户对象。此外,您还使用
Search.create!(Search\u params)
作为数据库中的一个资源,您需要保存这些搜索吗?嗨,@ZlatkoAlomerovic谢谢您的建议。我对rails不太了解,所以不确定初始化方法是什么,请您显示一个片段好吗?谢谢她还需要传入args/关键字args。在他的
create
methodHi@DR7上显示示例用法可能也会有所帮助我尝试了这个解决方案,但现在我在表单上的未知方法的搜索显示页面上出现错误,我将把我的显示页面添加到question@Kush该方法希望获得一个用户作为参数。请尝试添加
@search.user\u search\u合同(当前用户)。每个
@DR7仍然不起作用,并为#`@DR7获取错误
未定义的方法
用户搜索合同。这对他不起作用,因为他在视图中将其用作实例方法,而不是类方法。
.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
class Search < ActiveRecord::Base
  def self.user_search_contracts(user)
    user ||= User.new
    contracts = Contract.where(user_id: user.id)

    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