Searching and returning arrays

im a begginer in RoR and am using rails 3.2.3 and ruby 1.8.7

This forum has helped me to progress but I'm confused by one thing.

My app is supposed to allow seaching for results depending on the check
boxes that are checked. In my case, when the user checks a determined
facility and clicks "search" the corresponding hotels with only those
facilities should be returned. I can search by name/location and rating,
but can't search for facilities.

Briefly, my controller looks like:

def index

My view like:

<b>Facilities:</b><br />
<% for facility in Facility.find(:all) %>
  <div class="field">
  <%= check_box_tag "fc[]",>
  <%= facility.description %>
  <% end %>
<%= submit_tag "Search", :name => nil%>

<% @hotels.each do |hotel|%>
    <h2> <%= link_to, hotel %> </h2>

My hotels and facilities are a has_many_and_belongs_to relationship with
everything working. The facilities_hotels table is created with the
facility_id and hotel_id collums, and the facilities table has for
columns a facility_id and description(which is for e.g. pool, 24hroom
service etc)

The issue is in my model:


     if params
           arel = where('#searches for names/location/rating')

        if params[:fc].present?
          arel=arel.joins('INNER JOIN facilities_hotels ON ')

          for i in params[:fc].size
            arel=arel.where('facilities_hotels.facility_id = ?',
                #how do I increment on the previous line? Obviously
params([:fc][i]) does not work


I didn't want to do this with to_sql...

Also, when i run it, the query always returns empty results if I check a
facilities checkbox, but maybe that is a problem from that line of code
in my model but if you forsee an issue, I would appreciate a heads up in
terms of future code conflict relating to this issue

Thank you in advance

    if params\[:fc\]\.present?
      arel=arel\.joins\(&#39;INNER JOIN facilities\_hotels ON ')

      for i in params\[:fc\]\.size
        arel=arel\.where\(&#39;facilities\_hotels\.facility\_id = ?&#39;,

#how do I increment on the previous line? Obviously
params([:fc][i]) does not work

Typically I would just do

params[:fc].each do |fc_id|

To iterate over an array like that.

This doesn't do what you want though, since rails will be and-ing all
of the conditions
You probably want where(:facilities_hotels => {:facility_id =>
array_of_ids}) which will generate a IN clause