Maybe this could be your answer: def self.search(params) if params query = "SELECT * FROM Hotel where city LIKE '#{params[:search]}' OR name LIKE '#{params[:search]}'" query+= "AND rating = #{params[:rating]}" if params[:rating] @hotels = find_by_sql(query) arel else @hotels = Hotel.all end end
view: <%= form_tag hotels_path, :method =>'get' do%> <p> <b>Location:</b> <%= text_field_tag :search, params[:search]%><br /> <b>Rating:</b><br /> <%= radio_button_tag :rating, '5'%>5 Stars<br /> <%= radio_button_tag :rating, '4'%>4 Stars<br /> <%= radio_button_tag :rating, '3'%>3 Stars<br /> <%= radio_button_tag :rating, '6'%>Hostels<br /> <%= submit_tag "Search", :name => nil%> </p> <%end%> <% @hotels.each do |hotel| %> <p>hotel.name</p> <p>facilities</p> <% hotel.facility do |facility|%> <!-- likewise show your facility using facility.[name] over here--> <% end %> <% end %> On Wed, Apr 18, 2012 at 4:42 PM, Miguel A. <li...@ruby-forum.com> wrote: > I have a form that allows me to search a hotel by city or name and by > rating through radio buttons. It all works. > > However, my hotel model has_one :facility, this facility model is > composed by several boolean fields (for example, roomservice:boolean > restaurant:boolean and so on) > > The question is, I want to add checkbox fields for each facility I have > and in the search form, when the user selects it and presses Search, it > should only display the hotels which have those facilities. I've > searched but i can't put it to work. > > My hotel model: > > def self.search(params) > > if params > arel = where('city LIKE ? OR name LIKE ?', "%#{params[:search]}%", > "%#{params[:search]}%") > arel = arel.where('rating = ?', params[:rating]) if > params[:rating].present? > (what to put here?) > arel > else > all > end > end > My index view: > > <%= form_tag hotels_path, :method =>'get' do%> > <p> > <b>Location:</b> > <%= text_field_tag :search, params[:search]%><br /> > > > <b>Rating:</b><br /> > <%= radio_button_tag :rating, '5'%>5 Stars<br /> > <%= radio_button_tag :rating, '4'%>4 Stars<br /> > <%= radio_button_tag :rating, '3'%>3 Stars<br /> > <%= radio_button_tag :rating, '6'%>Hostels<br /> > > <b>Facilities:</b> > (what to put here?) > > <%= submit_tag "Search", :name => nil%> > </p> > <%end%> > ... > <% @hotels.each do |hotel|%> > > <div id="name"> > <h2> <%= link_to hotel.name, hotel %> </h2> > </div> > > > <div id="description"> > > <%= truncate(hotel.description, :length => 20) %> > </div> > > > <%end%> > > Thanks in advance, I can't comprehend how to it with checkboxes when > they can have multiple results. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.