简体   繁体   English

Ruby在轨道上。 兰萨克搜索协会

[英]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_contrequests_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM