'Refactoring to get attributes from self or parent

class Shop is potentially a child as it belongs_to :chain, , optional: true

The chain has chain_rules which apply to the shop if true.

To avoid re-writing patterns for a multiple attributes of both chain and shop, a class method

  def shop_rule
    if self.chain_id && self.chain.chain_rules
      self.chain
    else
      self
    end
  end

However in the console calling shop.shop_rule.attribute, the following error is generated: undefined method shop_rule' for #<Shop: ...`

How should this be defined then?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source