Ruby on rails 递归:包含在Rails ActiveRecord中
假设我有这些模型Ruby on rails 递归:包含在Rails ActiveRecord中,ruby-on-rails,activerecord,Ruby On 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 我怎么说我还想包括注释的用户呢?我认为:incl
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
我怎么说我还想包括注释的用户呢?我认为
:include=>{:comments=>:user}
应该可以工作。如果我已经包括了一些非递归的东西怎么办?我目前有:include=>:items,我需要将它与这样的东西结合起来,:include=>{:other_item=>:items}。可能吗?
p = Project.find(1, :include => :comments)
p.comments.collect(&:user).collect(&:name) # this executes select for each user