RAILS plugin delegate_belongs_to

can anyone give a link and sample about this delegate_belongs_to plugin in rails?

thank

+3
source share
1 answer

delegate_belongs_toseems to be replaced by pahanix delegates_attributes_to , but the main idea is this.

Say you have a model Officein your Rails application that has a field address, and you also have a model Employeethat relates to the office:

class Office < AcitveRecord::Base
end

class Employee < ActiveRecord::Base
  belongs_to :office
end

If you want to know the address of an employee, you will need to do the following:

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

However, if you used delegate_belongs_toas follows:

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

You will get direct access to the model attributes Office:

>> emp.address
=> "Edinburgh"

github . , faber github.

+11

Source: https://habr.com/ru/post/1741545/


All Articles