Rails delegate method with argument to separate class

If you need to delegate method to a custom class (not AR) and pass object as parameter to this class, you can use next code:
class One < ActiveRecord::Base
  # ...
  delegate :some_method, to: :name_for_the_class

  def name_for_the_class
    @delegator ||= ::ClassToDelegate.new(self)
  end
end

Поделиться с друзьями

Френдинг

 

Comments

No comments so far.

Leave a Reply

 
(will not be published)
 
 
Comment