2014-03-24 119 views
0

帖子属于讨论区,讨论区属于论坛。通过对象查找对象

说我有论坛对象:

forum #=> <Forum:#> 

从它,我应该怎么回属于该论坛的讨论的所有帖子?足够简单地返回属于论坛的所有讨论,但不包括通过讨论属于论坛的帖子。

(顺便说一句,我不是建立一个论坛只是一个例子)

回答

0

要获得第二级可以使用has_many :through 例如:

class Discussion < ActiveRecord::Base 
    has_many :posts 
end 

class Forum < ActiveRecord::Base 
    has_many :discussions 
    has_many :posts, through: :discussions 
end 

# get all posts in all discussions in the forum. 
Forum.find(1).posts