[英]Ruby on rails. Ransack search associations
I'm getting a little stuck with a ransack search form. 我对搜索包搜索表单有些困惑。
I need to search through a model's association, and bring results back when name_cont
BUT ONLY WHEN AN associated model's status == something
. 我需要搜索模型的关联,并且仅在关联模型的
status == something
时, name_cont
返回name_cont
结果。
I have a gig
model with a has_many
belongs_to
relationship with a request
model and user
model. 我有一个具有
has_many
belongs_to
关系与request
模型和user
模型的gig
模型。 The request
model has attributes status
and the user
model has attributes publicname
. request
模型具有status
属性,而user
模型具有属性publicname
。 I want to search publicname_cont
and only show results from the requests that have a status == "hired"
. 我想搜索
publicname_cont
,只显示status == "hired"
的请求的结果。
Currently my search brings back the results of publicname
regardless or the request
models status
. 目前,无论搜索结果或
request
模型status
如何,我的搜索都将返回publicname
的结果。
Gigs Controller: 演出控制器:
@gigs = @q.result(distinct: false).notexpired.order(date: :asc).page(params[:page]).per(20)
Search form: 搜索表格:
<%= search_form_for @q, url: welcome_index_url, remote: false do |f| %>
<%= text_field_tag :search, params[:search], class: 'loc-search', placeholder: "Location"%>
<%= f.search_field :locationname_cont, placeholder: "Bar name" %>
<%= f.search_field :requests_user_publicname_cont, placeholder: "Band name" %>
<%= f.submit %>
<% end %>
Currently, I can search requests_user_publicname_cont
and it gives me all the results as expected where the parameters match. 目前,我可以搜索
requests_user_publicname_cont
并在参数匹配的情况下按预期提供所有结果。 I am looking for a way to search requests_user_publicname_cont
AND requests_status_eq, ("hired")
我正在寻找一种方法来搜索
requests_user_publicname_cont
和requests_status_eq, ("hired")
I am not sure how to go about this, any help would be appreciated, thanks. 我不确定该如何处理,感谢您的帮助。
@q = Gig.joins(:requests).where(requests: { status: 'hired' }).ransack(params[:q])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.