0

I have this 2 tables in my DB:

room_type
+ id
+ adult_guess (int)

room
+ id
+ name
+ description
+ hotel_id
+ room_type_id

Now, I would like to do something like:

  Room.find(:all, :conditions => "hotel_id = " + @hotel_id, 
              :order_by => "room_type.adult_guess ASC").each do |room|
  end

I want to order my rooms by number of adult guess. How can I do that? I already set the relationships between rooms and room_types, I mean, I can do a

room.room_type.adult_guess

(if it helps on something).

Thank you

content01
  • 3,115
  • 6
  • 41
  • 61

2 Answers2

0

Try using :joins to make sure that relation is included.

Room.find(:all, :joins => :room_types, :conditions => "hotel_id = " + @hotel_id, 
          :order_by => "room_type.adult_guess ASC").each do |room|

and see if that helps.

Read more about :include vs :join when dealing with ActiveRecord queries here: Rails :include vs. :joins

Community
  • 1
  • 1
Steph Rose
  • 2,126
  • 3
  • 23
  • 35
0

If you don't need to get access to your room_types table use :joins, but if you want to show that information - use :include:

Room.all(:conditions => ['rooms.hotel_id = ?', @hotel_id], :joins => :room_types, :order => "room_types.adult_guest ASC").each do |room|
  ...
end
Oleksandr Skrypnyk
  • 2,762
  • 1
  • 20
  • 25