2009-09-15 83 views
4

說我有這些模型遞歸:包括在Rails的ActiveRecord的

class Project < ActiveRecord::Base 
    has_many :comments 
end 

class Comment < ActiveRecord::Base 
    belongs_to :project 
    belongs_to :user 
end 

class User < ActiveRecord::Base 
    has_many :comments 
end 

所以,我可以做

p = Project.find(1, :include => :comments) 
p.comments.collect(&:user).collect(&:name) # this executes select for each user 

我怎麼說我也想有評論的用戶?

回答

10

我認爲:include => {:comments => :user}應該工作。

+0

如果我已經包含不遞歸的東西,該怎麼辦? 我目前有:include =>:items,我需要將它與這樣的東西結合起來:include => {:other_item =>:items}。 這可能嗎? – Michael