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
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With