Search multiple models at once with Ransack
Okay, after asking the question the answer popped into my head.
Instead of the search_form_for
helper I'm now just using the form_tag
helper in the following way:
<%= form_tag search_path, method: :get do %>
<%= text_field_tag :q, params[:q] %>
<%= end %>
and in the search action I just do:
q = params[:q]
@works = Work.search(name_cont: q).result
@projects = Project.search(name_cont: q).result
@users = User.search(name_cont: q).result
This works for me. I hope this also helps someone else.
How to search multiple models with ransack in liquid templates
This answer is pertinent to the ransack
portion as I have no experience with liquid and it does not seem to pertain the the question at hand
I am assuming the following
class Course < ApplicationRecord
belongs_to :teacher
has_many :articles
end
You should be able to use a compound attribute chain like title_or_teacher_firstname_or_articles_title_cont
e.g.
<form class="search" method="get" action="{{ request.url_helpers.courses_path }}">
<input type="text" placeholder="Search" name="q[title_or_teacher_firstname_or_articles_title_cont]" value="" />
<input type="submit" value="Search" />
</form>
Also your controller code is traditionally represented as:
class SearchController < ApplicationController
def index
@q = Course.ransack(params[:q])
@courses = @q.result
end
end
Searching Multiple Models with Ransack Rails 4
The output you are getting is correct. Each of those variables contains an ActiveRecord_Relation
object which can be treated like an array. Normally you'd do something like:
<% @items.each do |item| %>
<%= item.name %> # or whatever
<% end %>
<% @users.each do |user| %>
# and so on
Alternatively, you could combine your results @results = @items + @booths + @users
and then:
<% @results.each do |result| %>
# display the result
<% end %>
Rails Ransack gem: search for multiple values with one condition
If you look over Ransack's Search Matchers you will see one with *_in - match any values in array
, which is what you need if you want to search in arrays.
Because your event_ids
can come in either as a string or an array and *_in
requires and array, we have to make sure we always feed it an array.
[event_ids].flatten # returns an array all the time
The query below should works properly now.
ransack("job_name_cont" => job_name, "event_id_in" => [event_ids].flatten).result
Search two models with Ransack Parameters
You can access that value with
facility_id = params[:q]['facility_id_eq']
@facility = Facility.find(facility_id)
Related Topics
Find() with Nil When There Are No Records
How to Use Jquery-Tokeninput and Acts-As-Taggable-On
Is Inject the Same Thing as Reduce in Ruby
Ruby: Proc.New { 'Waffles' } VS. Proc { 'Waffles' }
Properly Converting a Cmyk Image to Rgb with Rmagick
Shorten Long Numbers to Include K/M/B/T
Kernel#Gets Attempts to Read File Instead of Standard Input
Converting Utf8 to Ansi with Ruby
Best Ruby Idiom for "Nil or Zero"
How to Find If a String Starts with Another String in Ruby
Ruby Methods and Optional Parameters
What Does the Fail Keyword Do in Ruby
Install Bundler Gem Using Ansible
Subtract N Hours from a Datetime in Ruby
Difference Between 'Self.Method_Name' and 'Class << Self' in Ruby