title: Ruby on Rails - Where clause in multiple tables
body: <p>Using rails 3.2.3.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I am able to search by the first two but can't search by facilities (they are checkboxes on the search form).</p>
  
  <p>View code:</p>
  
  <pre><code>&lt;%= form_tag hotels_path, :method =&gt;'get' do%&gt;
          &lt;p&gt;
              &lt;b&gt;Location:&lt;/b&gt;
              &lt;%= text_field_tag :search, params[:search]%&gt;&lt;br /&gt;&lt;br /&gt;
  
  
              &lt;b&gt;Rating:&lt;/b&gt;&lt;br /&gt;
              &lt;%= radio_button_tag :ranking, '5'%&gt;5 Stars&lt;br /&gt;
              &lt;%= radio_button_tag :ranking, '4'%&gt;4 Stars&lt;br /&gt;
              &lt;%= radio_button_tag :ranking, '3'%&gt;3 Stars&lt;br /&gt;
              &lt;%= radio_button_tag :ranking, '10'%&gt;Hostels&lt;br /&gt;&lt;br /&gt;
  
  
              &lt;b&gt;Facilities:&lt;/b&gt;&lt;br /&gt;
              &lt;% for facility in Facility.find(:all) %&gt;
                  &lt;div class="field"&gt;
                      &lt;%= check_box_tag "fc[]", facility.id%&gt;
                      &lt;%= facility.description %&gt;
                  &lt;/div&gt;
              &lt;% end %&gt;
  
              &lt;%= submit_tag "Search", :name =&gt; nil%&gt;
          &lt;/p&gt;
          &lt;%end%&gt;
  </code></pre>
  
  <p>hotel controller:</p>
  
  <pre><code> def index
      @hotels= Hotel.search(params)
  
      respond_to do |format|
        format.html
        format.json { render :json =&gt; @hotels }
      end
    end
  </code></pre>
  
  <p>hotel model:</p>
  
  <pre><code> 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])) -&gt; i dont know what to put here
  
        arel
       else
         all
       end
      end
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help is highly appreciated</p>