Ruby on Rails - Where clause in multiple tables

multiple-tables, ruby-on-rails, where-clause

Solution

You need to add a joins clause to get it to join to the other table.

.joins(:facilities_hotels).where('facilities_hotels.facility_id = ?', params[:fc])

Problem

Using rails 3.2.3. This is driving me crazy. I know that active record is supposed to simplify these types of actions but I can't get my head around it. I have a Hotel and Facility model. Both have a has_and_belongs_to_many relation. A hotel can have many facilities and my goal is to search a hotel by its name/location, rating and facilities. I am able to search by the first two but can't search by facilities (they are checkboxes on the search form). View code: ``` <%= form_tag hotels_path, :method =>'get' do%> <p> <b>Location:</b> <%= text_field_tag :search, params[:search]%><br /><br /> <b>Rating:</b><br /> <%= radio_button_tag :ranking, '5'%>5 Stars<br /> <%= radio_button_tag :ranking, '4'%>4 Stars<br /> <%= radio_button_tag :ranking, '3'%>3 Stars<br /> <%= radio_button_tag :ranking, '10'%>Hostels<br /><br /> <b>Facilities:</b><br /> <% for facility in Facility.find(:all) %> <div class="field"> <%= check_box_tag "fc[]", facility.id%> <%= facility.description %> </div> <% end %> <%= submit_tag "Search", :name => nil%> </p> <%end%> ``` hotel controller: ``` def index @hotels= Hotel.search(params) respond_to do |format| format.html format.json { render :json => @hotels } end end ``` hotel model: ``` def self.search(params) if params arel = where('city LIKE ? OR name LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%") arel = arel.where('ranking = ?', params[:ranking]) if params[:ranking].present? #arel = arel.where (' = ?', params([:fc])) -> i dont know what to put here arel else all end end ``` Basically it needs to fetch the name/location, narrow it by ranking and then narrow it even more by the selected checkboxes. I can't get the last one. The name/location and rating are in the hotels table but each hotel's facilities are in the facilities_hotels joint table (that table has for keys the hotel_id and the facility_id) By the way, I am able to create a hotel with all the facilities I want, so the relation between the two is correct. Any help is highly appreciated

Original source