递归:包含在Rails ActiveRecord中

Jak*_*old 4 activerecord ruby-on-rails

说我有这些模型

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
Run Code Online (Sandbox Code Playgroud)

这样我才能做到

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

我怎么说我还想包括评论的用户?

mru*_*ueg 10

我认为:include => {:comments => :user}应该工作.