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
.
I have a gig
model with a has_many
belongs_to
relationship with a request
model and user
model. The request
model has attributes status
and the user
model has attributes publicname
. I want to search publicname_cont
and only show results from the requests that have a status == "hired"
.
Currently my search brings back the results of publicname
regardless or the request
models status
.
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.
I am looking for a way to search requests_user_publicname_cont
AND 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])