2012-05-17 69 views
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 

我可以做以下chain named_scopes跨模型? (ActiveRecord的,Rails)的

a = ModelA.find(1) 

a.modelb.modelc.due_on(today).modeld.has_feature(somefeature).should return_an_array_of_instances_of_ModelD 

如果沒有,什麼是從MODELA對象引用相關modeld對象的最佳實踐。

回答