Class ModelA < ActiveRecord::Base
has_many :modelb
end
Class ModelB < ActiveRecord::Base
has_many :modelc
end
Class ModelC < ActiveRecord::Base
has_many :modeld
named_scope :due_on, lambda {|date| { :conditions => {:due_date => date}}}
end
Class ModelD
named_scope :has_feature, lambda {|feature| { :conditions => {:feature => feature}}}
end
我可以做以下
a = ModelA.find(1)
a.modelb.modelc.due_on(today).modeld.has_feature(somefeature).should return_an_array_of_instances_of_ModelD
如果没有,从modela对象引用相关的modeld对象的最佳做法是什么。
答案 0 :(得分:0)
您可能希望避免违反Demeter法则,并为其创建抽象。
即。 a.modelb.ending(today).with_feature(feature)
很高兴看到它:http://devblog.avdi.org/2011/07/05/demeter-its-not-just-a-good-idea-its-the-law/