method

attributes

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

szeryf · Dec 28, 20094 thanks

Another possible gotcha -- the returned hash keys are of type String, not Symbol:

user.attributes["login"] # => "joe"
user.attributes[:login] # => nil

Expensive method!

szeryf · Jun 18, 20093 thanks

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

tadman · Jun 25, 20091 thank

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.