Rails has two great ways to avoid breaking Demeter’s law in models.
The first is:
class Restaurant < ActiveRecord::Base
belongs_to :franchise
delegate :owner, to: :franchise
end
The second is the following:
class Restaurant < ActiveRecord::Base
belongs_to :franchise
has_one :owner, through: :franchise
end
What is the difference? Is there anything to recommend one option over another in some or all cases?
The only difference I can find is that the option delegateseems to generate two SQL queries to retrieve the last record, while it belongs_to :throughseems to do this in one query.
source
share