Ransack,查找具有所有相关记录的记录

时间:2015-11-17 21:35:57

标签: ruby-on-rails ransack

我有一个配方模型,其中包含很多成分,每种成分都属于一个项目。在我的高级搜索表单中,我希望用户选择多种成分,让Ransack找到包含用户选择的所有成分的配方。

我尝试了以下搜索字段:

= f.collection_select(:ingredients_item_id_in, Item.all, :id, :name, {}, {multiple: true})

但从逻辑上讲,这会导致显示的所有食谱都包含任何选定的成分。

:ingredients_item_id_in更改为:ingredients_item_id_in_all会导致查询错误,因为一条记录不能包含多个item_id值。

有关在Ransack中创建此搜索参数的任何想法,还是应该为此创建子查询?

根据要求,我的控制器搜索方法:

  def search
    @q = Recipe.ransack(params[:q])
    @recipes = @q.result(distinct: true).include_related_models.published
  end

1 个答案:

答案 0 :(得分:1)

我最近遇到了类似的项目任务( Rails 4.2.4 / Ruby 2.3.1 )。

庄园有许多舒适。我需要得到所有的庄园,其中包括所有选定的舒适。

以下是我使用Ransack

解决问题的方法

在我的情况下,我有has_many :through关系。

<强> estate.rb

class Estate < ActiveRecord::Base
  has_many :estate_comforts
  has_many :comforts, through: :estate_comforts
end

<强> comfort.rb

class Comfort < ActiveRecord::Base
  has_many :estate_comforts
  has_many :estates, through: :estate_comforts
end

<强> estate_comfort.rb

class EstateComfort < ActiveRecord::Base
  belongs_to :estate
  belongs_to :comfort
end

对于复杂查询,您需要通过post进行搜索。为此你必须编辑这样的路线。并将search操作添加到estates_controlle.rb。有关更多信息,请阅读Ransack wiki

<强>的routes.rb

...
resources :estates
  collection do
    match 'search' => 'estates#search', via: %i[get post], as: :search
  end
end

<强> estates_controller.rb

class EstatesController < ApplicationController
  ...

  def index
    @q = Estate.ransack(params[:q])

    @estates =
      if params[:q]&.has_key?(:estate_comforts_comfort_id_eq_any)
        # Store checked comforts
        session[:estate_comforts_comfort_id_eq_any] = params[:q][:estate_comforts_comfort_id_eq_any]

        comforts_count = params[:q][:estate_comforts_comfort_id_eq_any].count
        ids = @q.result.includes(:estate_comforts).group_by(&:id).select { |_, v| v.count == comforts_count}.keys
        Estate.where(id: ids)
      else
        @q.result(distinct: true)
      end
  end

  def search
    index
    render :index
  end
end

最后是模板部分......

<强>屋/ index.haml

= search_form_for @q, url: search_estates_path, html: { method: :post } do |f|
  # here goes the form inputs

  # Polulate checkboxes with previously checked comforts 
  - Comfort.find_each do |comfort|
    # Was checked previously?
    - checked = comfort.id.to_s.in?(session[:estate_comforts_comfort_id_eq_any].to_a)
    %div  
      %input{ name: 'q[estate_comforts_comfort_id_eq_any][]',
              type: "checkbox",
              id: "checkbox#{comfort.id}",
              value: comfort.id,
              checked: checked }
        %label{for: "checkbox#{comfort.id}"}= comfort.name

将生成以下html

<form class="estate_search" id="estate_search" action="/estates/search" accept-charset="UTF-8" method="post">
  <div>
    <input checked="" id="checkbox1" name="q[estate_comforts_comfort_id_eq_any][]" type="checkbox" value="1">
    <label for="checkbox1">Comfort Name 1</label>
  </div>
  <div>
    <input id="checkbox2" name="q[estate_comforts_comfort_id_eq_any][]" type="checkbox" value="2">
    <label for="checkbox2">Comfort Name 2</label>
  </div>
</form>