instance method
define_attribute_methods
Ruby on Rails 8.1.2
Since v3.0.20Signature
define_attribute_methods(*attr_names)
Declares the attributes that should be prefixed and suffixed by ActiveModel::AttributeMethods.
To use, pass attribute names (as strings or symbols). Be sure to declare define_attribute_methods after you define any prefix, suffix, or affix methods, or they will not hook in.
class Person include ActiveModel::AttributeMethods attr_accessor :name, :age, :address attribute_method_prefix 'clear_' # Call to define_attribute_methods must appear after the # attribute_method_prefix, attribute_method_suffix or # attribute_method_affix declarations. define_attribute_methods :name, :age, :address private def clear_attribute(attr) send("#{attr}=", nil) end end
Parameters
-
attr_namesrest
Source
# File activemodel/lib/active_model/attribute_methods.rb, line 272
def define_attribute_methods(*attr_names)
ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner|
attr_names.flatten.each do |attr_name|
define_attribute_method(attr_name, _owner: owner)
aliases_by_attribute_name[attr_name.to_s].each do |aliased_name|
generate_alias_attribute_methods owner, aliased_name, attr_name
end
end
end
end
Defined in activemodel/lib/active_model/attribute_methods.rb line 272
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveModel::AttributeMethods::ClassMethods