gpt4 book ai didi

ruby-on-rails - Ransack,查找具有所有相关记录的记录

转载 作者:行者123 更新时间:2023-12-04 04:38:02 27 4
gpt4 key购买 nike

我有一个配方模型,该模型具有很多成分,每种成分都属于一个项目。在我的高级搜索表单中,我希望用户选择多种成分,然后让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中创建此搜索参数的任何想法,还是应该为此创建子查询?

根据要求,我的 Controller 搜索方法:
  def search
@q = Recipe.ransack(params[:q])
@recipes = @q.result(distinct: true).include_related_models.published
end

最佳答案

我最近在我的项目中遇到了类似的任务( 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

最后是模板部分...

estates/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>

关于ruby-on-rails - Ransack,查找具有所有相关记录的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33767461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com