Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:13:27+00:00 2026-06-03T02:13:27+00:00

I’m a beginner in RoR and am using rails 3.2.3 and ruby 1.8.7 This

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:13:28+00:00Added an answer on June 3, 2026 at 2:13 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.