Skip to content

Instantly share code, notes, and snippets.

@NikitaAvvakumov
Created October 3, 2014 09:32
Show Gist options
  • Save NikitaAvvakumov/4de814072d642ef05e16 to your computer and use it in GitHub Desktop.
Save NikitaAvvakumov/4de814072d642ef05e16 to your computer and use it in GitHub Desktop.
Orphan OSRA number
# Solution 1
class Partner < ActiveRecord::Base
belongs_to :province
has_many :orphan_lists
delegate :code, to: :province, prefix: true
end
class Orphan < ActiveRecord::Base
belongs_to :orphan_list
delegate :partner, to: :orphan_list
province_code = self.partner.province_code
end
# Solution 2
class Partner < ActiveRecord::Base
belongs_to :province
has_many :orphan_lists
delegate :code, to: :province, prefix: true
end
class OrphanList < ActiveRecord::Base
belongs_to :partner
delegate :province_code, to: :partner, prefix: true
end
class Orphan < ActiveRecord::Base
belongs_to :orphan_list
delegate :partner_province_code, to: :orphan_list
end
@PurityControl
Copy link

I really like the idea of the delegate method, but given the level of chaining here what about

province_code = self.orphan_list.partner.province_code

I think it makes the intention much clearer unless you are going to need access to other properties too, in which case the delegate macros clearly win out.

Is this likely to be called for a single partner or for a query retrieving a collection of partners?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment