开发者

Association function in Rails

开发者 https://www.devze.com 2023-02-01 18:23 出处:网络
I have Users, Hosts, and Checkins. Users -> has_many -> Hosts -> has_many -> Checkins Checkin -> belongs_to 开发者_开发知识库-> Host -> belongs_to -> User

I have Users, Hosts, and Checkins.

Users -> has_many -> Hosts -> has_many -> Checkins

Checkin -> belongs_to 开发者_开发知识库-> Host -> belongs_to -> User

How can I easily do a query for all checkins for a particular user without writing my own SQL?


Use a has_many :through association, like this:

has_many :checkins, :through => :hosts

You can then simply use checkins as your direct association. Whoo hoo!

0

精彩评论

暂无评论...
验证码 换一张
取 消