任何人都可以提供有关此插件的引用和示例 delegate_belongs_to 在 rails 上?
谢谢
最佳答案
delegate_belongs_to
好像已经换成了pahanix's delegates_attributes_to ,但基本思想如下。
假设您有一个 Office
您的 Rails 应用中的模型,它具有 address
字段,您还有一个 Employee
属于办公室的模型:
class Office < AcitveRecord::Base
end
class Employee < ActiveRecord::Base
belongs_to :office
end
如果您想找出员工的地址,您必须执行以下操作:
>> emp.office.address
=> "Edinburgh"
但是,如果您使用过
delegate_belongs_to
像这样:class Employee < ActiveRecord::Base
belongs_to :office
delegate_belongs_to :office
end
您将可以直接访问
Office
的属性。模型:>> emp.address
=> "Edinburgh"
github page详细介绍了使用该插件的其他一些方法,值得一读。原始插件似乎位于 faber's github page .
关于ruby-on-rails - RAILS 插件 delegate_belongs_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2656850/