Created
October 3, 2014 09:32
-
-
Save NikitaAvvakumov/4de814072d642ef05e16 to your computer and use it in GitHub Desktop.
Orphan OSRA number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?