Association function in Rails
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!
精彩评论