Flowdock
method

attributes

Importance_3
v1.1.6 - Show latest stable - 3 notes - Class: ActiveRecord::Base
attributes(options = nil) public

Returns a hash of all the attributes with their names as keys and clones of their objects as values.

Show source
Register or log in to add new notes.
December 28, 2009
4 thanks

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
June 18, 2009
3 thanks

Expensive method!

This method builds the a new hash every time it’s called, so be cautious not to use it in loops etc.

June 25, 2009
1 thank

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.