我有以下关联。
class Farm < ActiveRecord::Base
has_many :crops
end
class Crop < ActiveRecord::Base
belongs_to :farm
has_many :seed_batches
end
class SeedBatch < ActiveRecord::Base
belongs_to :crop
has_many :tasks, through: :task_batches
end
class Task < ActiveRecord::Base
has_many :seed_batches, through: :task_batches
end
class TaskBatch < ActiveRecord::Base
belongs_to :task
belongs_to :seed_batch
end
本质上,一个农场有很多农作物。每种农作物都有许多批次的种子。每个种子批次都有许多任务。
我的问题是:我如何才能知道农场的ID?
我尝试了多种方法来进行.where()搜索,但是都出现了错误。有人可以启发我吗?
尝试
Farm.find(1).crops.each(&:seed_batches).collect(&:tasks)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句