使用 Rails Helpers 连接和排序两个数据库表
我的数据库中有这两张表:
room_type
+ id
+ adult_guess (int)
room
+ id
+ name
+ description
+ hotel_id
+ room_type_id
现在,我想做类似的事情:
Room.find(:all, :conditions => "hotel_id = " + @hotel_id,
:order_by => "room_type.adult_guess ASC").each do |room|
end
我想根据成人猜测的数量来订购我的房间。我怎样才能做到这一点? 我已经设置了房间和 room_types 之间的关系,我的意思是,我可以做一个
房间.房间类型.adult_guess
(如果它对某些事情有帮助)。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不需要访问
room_types
表,请使用:joins
,但如果您想显示该信息 - 请使用:include
: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
:尝试使用
:joins
确保包含该关系。看看是否有帮助。
在此处阅读有关处理 ActiveRecord 查询时 :include 与 :join 的更多信息:
Rails :include 与 :joins
Try using
:joins
to make sure that relation is included.and see if that helps.
Read more about :include vs :join when dealing with ActiveRecord queries here:
Rails :include vs. :joins