views:

57

answers:

2

Hi there,

anyone could give reference and sample about this plugin delegate_belongs_to in rails?

Thanks

A: 

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.

gar
A: 

Thanks Gar, you're good!

iwan