Allows access to the object attributes, which are held in the
@attributes hash, as though they were first-class methods. So a Person class with a name attribute can use
Person#name and Person#name= and never directly use the attributes hash
— except for multiple assigns with ActiveRecord#attributes=. A
Milestone class can also ask Milestone#completed? to test that the
completed attribute is not nil or 0.
It’s also possible to instantiate related objects, so a Client class belonging to the clients table with a
master_id foreign key can instantiate master through
Client#master.
# File activerecord/lib/active_record/attribute_methods.rb, line 234
def method_missing(method_id, *args, &block)
if method_id == :to_ary || method_id == :to_str
raise NoMethodError, "undefined method `#{method_id}' for #{inspect}:#{self.class}"
end
method_name = method_id.to_s
if self.class.private_method_defined?(method_name)
raise NoMethodError.new("Attempt to call private method", method_name, args)
end
# If we haven't generated any methods yet, generate them, then
# see if we've created the method we're looking for.
if !self.class.generated_methods?
self.class.define_attribute_methods
if self.class.generated_methods.include?(method_name)
return self.send(method_id, *args, &block)
end
end
if self.class.primary_key.to_s == method_name
id
elsif md = self.class.match_attribute_method?(method_name)
attribute_name, method_type = md.pre_match, md.to_s
if @attributes.include?(attribute_name)
__send__("attribute#{method_type}", attribute_name, *args, &block)
else
super
end
elsif @attributes.include?(method_name)
read_attribute(method_name)
else
super
end
end