Flowdock
method

attributes

Importance_3
Ruby on Rails latest stable (v6.1.7.7) - 3 notes - Class: ActiveRecord::Base

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v3.1.0) is shown here.

These similar methods exist in v6.1.7.7:

attributes() public

Returns a hash of all the attributes with their names as keys and the values of the attributes 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.