3
我有嵌套has_many關聯導航嵌套的has_many
項目有很多部分 部分有許多任務 任務有許多工作
有沒有更好的方式來獲得比
關聯到一個項目中的所有作業project.parts.each do |p|
p.tasks.each do |t|
t.jobs.each do |j|
...
end
end
end
感謝
我有嵌套has_many關聯導航嵌套的has_many
項目有很多部分 部分有許多任務 任務有許多工作
有沒有更好的方式來獲得比
關聯到一個項目中的所有作業project.parts.each do |p|
p.tasks.each do |t|
t.jobs.each do |j|
...
end
end
end
感謝
你可能會與添加has_many
協會選項,而您在Project
模型中定義#jobs
方法。
例如:
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
謝謝,我可能會嘗試類似的東西。我實際上有4個模型嵌套在一起(不是三個),但這個想法是一樣的。 – macsig