0

I'm a beginner 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 searching 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
    @hotels= Hotel.search(params)
(...)
end

My view like:

<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%>
(...)
<% @hotels.each do |hotel|%>
    <h2> <%= link_to hotel.name, 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 columns, 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:

def self.search(params)

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

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

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


     else
       all
     end

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

1
  • Did you check the query generated in the development log see if the sql query is correct? Commented Apr 23, 2012 at 18:32

1 Answer 1

2

Here's your problem, chaining where merges the where conditions with " AND ". Check your development.log file, or test.log if you are doing TDD (which you should be). This will have the SQL ActiveRecord generate. (I'll bet it works when you only check one box.)

First, check your assumption that params[:fc] is an array of the checked boxes. I'll often throw an exception and look at the page returned to the browser.

def self.search(params)
  raise params[:fc].inspect

If this is the array that you think it is, then your where method should be:

where('facilities_hotels.facility_id in (:fc_list)', :fc_list => params[:fc])

As a coding tip, consider whether you think the following code is cleaner.

def self.search(params)
  if params && params[:fc].present?
    joins('INNER JOIN facilities_hotels ON hotels.id=facilities_hotels.hotel_id ').
      where('facilities_hotels.facility_id in (:fc_list)', :fc_list => params[:fc])
  else
    Hotel
  end
end
Sign up to request clarification or add additional context in comments.

3 Comments

Before your code and with only one checkbox, my code executed, but returned nil. And when i checked 2 boxes or more, it crashed. Now it works, appart from one thing. If I check 2 checkboxes, all the hotels appear. The ones that have that one facility and those that have those two. And I only want it to show those which have both of the selected facilities. Any help? (if I change the 'in' to '=' it won't work because it only expects one param and i'll be passing two if I check two boxes)
That's a lot tricker. It would take some thought and I might come up with something better, but off hand, you might do a group("hotels.id"), and having("count(facility_id) = :count", :count => params[:fc].size)
Just wanted to let you know that your solution worked, just had to addapt to a new query and minor tweeks. Thanks so much Marlin Pierce :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.