开发者

RAILS plugin delegate_belongs_to

anyone could give reference and sample about this pl开发者_高级运维ugin delegate_belongs_to in rails?

Thanks


delegate_belongs_to seems to have been replaced by pahanix's delegates_attributes_to, but the basic idea is as follows.

Say you have an Office model in your Rails app which has an address field, and you also have an Employee model which belongs to an office:

class Office < AcitveRecord::Base
end

class Employee < ActiveRecord::Base
  belongs_to :office
end

If you want to find out the address of an employee you would have to do the following:

>> emp.office.address
=> "Edinburgh"

However, if you had used delegate_belongs_to like so:

class Employee < ActiveRecord::Base
  belongs_to :office
  delegate_belongs_to :office
end

you would get direct access to the attributes of the Office model:

>> emp.address
=> "Edinburgh"

The github page details a number of other ways of using the plugin and is worth reading. The original plugin seems to have resided at faber's github page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜