1
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

如果不是,那么从模型对象引用相关建模对象的最佳实践是什么。

4

1 回答 1

0

您可能希望避免违反得墨忒耳定律,并为其创建抽象。

IEa.modelb.ending(today).with_feature(feature)

很好读:http ://devblog.avdi.org/2011/07/05/demeter-its-not-just-a-good-idea-its-the-law/

于 2012-05-17T03:50:50.063 回答