Reputation: 5948
I have nested has_many associations
Project has many Parts Part has many Tasks Task has many jobs
Is there a better way to get all the jobs associated to a project than
project.parts.each do |p|
p.tasks.each do |t|
t.jobs.each do |j|
...
end
end
end
Thanks
Upvotes: 3
Views: 85
Reputation: 1083
You might add an has_many
association with the through
option, while you define a #jobs
method in your Project
model.
For instance :
class Project < ActiveRecord::Base
has_many :parts
has_many :tasks, through: :parts
def jobs
jobs = []
tasks.each {|t| jobs << t.jobs }
jobs.flatten
end
end
Upvotes: 1