【问题标题】:search ruby on rails only for the current_users records仅为 current_users 记录在 Rails 上搜索 ruby
【发布时间】:2019-04-04 17:16:48
【问题描述】:

我有一个高级搜索页面,其中包含用于我的合同的单独搜索控制器。

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

我正在使用设计。

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

【问题讨论】:

  • 向 Search 模型添加初始化方法并向其发送 current_user 对象。另外,您使用Search.create!(search_params) 作为数据库中的资源,您需要保存这些搜索吗?
  • 嗨,@ZlatkoAlomerovic 感谢您的建议。我是 Rails 新手,所以不确定什么是初始化方法,请你显示一个 sn-p 吗?谢谢

标签: sql ruby-on-rails ruby devise


【解决方案1】:

虽然,我不会以这种方式实现这个搜索功能,但是......

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

class Search < ActiveRecord::Base
  def user_search_contracts(user)
    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

在你的视野中 ->

.wrapper_with_padding
  #houses.clearfix
    - unless @search.user_search_contracts(current_user).blank?
      - @search.user_search_contracts(current_user).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 methods vs. Instance methods

【讨论】:

    【解决方案2】:

    你可以为你的搜索模型做一个类方法。

    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
    

    【讨论】:

    • 他还需要传入参数/关键字参数。在他的create 方法上显示示例用法也可能会有所帮助
    • 嗨@DR7 我尝试了这个解决方案,但现在我的搜索显示页面上出现错误,因为表单上的未知方法,我会将我的显示页面添加到问题中
    • @Kush 该方法期望获取用户作为参数。尝试添加@search.user_search_contracts(current_user).each
    • @DR7 仍然无法正常工作并收到错误 undefined method user_search_contracts' for #<0xfb085c0>
    猜你喜欢
    • 2016-05-24
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    相关资源
    最近更新 更多