alias_attribute

alias_attribute(new_name, old_name)
Instance Public methods

Allows you to make aliases for attributes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Person
  include ActiveModel::AttributeMethods
 
  attr_accessor :name
  attribute_method_suffix '_short?'
  define_attribute_methods :name
 
  alias_attribute :nickname, :name
 
  private
 
  def attribute_short?(attr)
    send(attr).length < 5
  end
end
 
person = Person.new
person.name = 'Bob'
person.name            # => "Bob"
person.nickname        # => "Bob"
person.name_short?     # => true
person.nickname_short? # => true
doc_ruby_on_rails
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.