Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive :include in Rails ActiveRecord

Say I have these models

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

So that I can do

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

How do I say I want to also include comment's user?

like image 401
Jakub Arnold Avatar asked Dec 29 '22 15:12

Jakub Arnold


1 Answers

I believe :include => {:comments => :user} should work.

like image 90
mrueg Avatar answered Jan 09 '23 07:01

mrueg