How to define a custom name for delegate method in Rails 5?
Asked Answered
T

1

5

I have a model with a delegate method,

class Card < ApplicationRecord
  has_one :meta_sm2

  delegate :next_repetition,
           to: :meta_sm2
end

Because the underlying model (currently is meta_sm2) might change in the future, so I would like to make the delegate method next_repetition to a custom name, like priority for example.

How can I defined a custom name delegate method, so that I can call the next_repetition like card.priority?

Trotyl answered 18/2, 2017 at 4:1 Comment(2)
How would you like to call the custom name? I.e. considering I have a object from Card class called card.Pet
I have updated the questionTrotyl
A
14

You can use alias_method to achieve this.

class Card < ApplicationRecord
  has_one :meta_sm2

  delegate :next_repetition,
           to: :meta_sm2

alias_method :priority, :next_repetition

end
Ahl answered 18/2, 2017 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.