instance method
alias_attribute
Ruby on Rails 8.1.2
Since v7.1.6Signature
alias_attribute(new_name, old_name)
Allows you to make aliases for attributes.
class Person < ActiveRecord::Base alias_attribute :nickname, :name end person = Person.create(name: 'Bob') person.name # => "Bob" person.nickname # => "Bob"
The alias can also be used for querying:
Person.where(nickname: "Bob") # SELECT "people".* FROM "people" WHERE "people"."name" = "Bob"
Parameters
-
new_namereq -
old_namereq
Source
# File activerecord/lib/active_record/attribute_methods.rb, line 66
def alias_attribute(new_name, old_name)
super
if @alias_attributes_mass_generated
ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |code_generator|
generate_alias_attribute_methods(code_generator, new_name, old_name)
end
end
end
Defined in activerecord/lib/active_record/attribute_methods.rb line 66
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::AttributeMethods::ClassMethods