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 7816849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:01:27+00:00 2026-06-02T06:01:27+00:00

Using rails 3.2.3. This is driving me crazy. I know that active record is

  • 0

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

  • 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-02T06:01:28+00:00Added an answer on June 2, 2026 at 6:01 am

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

Sidebar

Related Questions

Anybody know how to produce an xml element like this in rails using xml
I would like a little confirmation that I'm doing this correctly. Using rails single
I am using Rails 3 and getting an error that looks like this: undefined
I'm using rails 2 for this app, with ActionMailer, but this is a general
I'm using Rails 3.1 and following this railscast to implement active merchant, I'm trying
I am trying to learn Ruby on Rails using this Rails Tutorial. Earlier, I
Hey, I'm using Rails 2.3.5 and using rufus scheduler to send periodic emails. This
I am new to both DBs and Rails (using 3.2), and I suspect this
This question Check all associations before destroy in rails suggests using :dependent => :restrict
This one might be a little confusing. I'm using AMCharts with rails. Amcharts comes

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.