method
attributes
v1.0.0 -
Show latest stable
- Class:
ActiveRecord::Base
attributes()public
Returns a hash of all the attributes with their names as keys and clones of their objects as values.
3Notes
Attribute names are Strings, not Symbols
Another possible gotcha -- the returned hash keys are of type String, not Symbol:
user.attributes["login"] # => "joe"
user.attributes[:login] # => nil
Expensive method!
This method builds the a new hash every time it's called, so be cautious not to use it in loops etc.
Returns a copy of the attribute contents
As szeryf notes, this is a really expensive method, but another important remark is that the contents returned are a copy of the actual values.
model.attributes['name'] # => 'Joe'
model.attributes['name'] = 'Jim'
model.attributes['name'] # => 'Joe' still
model.name # => 'Joe'
This has the potential to be confusing as you're given the impression you have direct access to the attributes.