Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Make AR::Base#changed_attributes to return indifferent hash #10589

wants to merge 1 commit into from

7 participants


changed_attributes hash with symbols as keys has better semantics.


It is required to return HashWithIndifferentAccess instead of just plain regular Hash???
I mean what value does it bring in to the code???


changed_attributes hash with symbols as keys has better semantics.

Can you elaborate on this?


Well I expect Symbol to mean a method name or a constant and String to mean free text.

In this case hash keys are referencing a method, so symbols looks better.
You don't write has_many "posts", right? Because :posts is a method.


I don't think we should make it indifferent access, since AR attributes also doesn't return one:

>> Post
=> Post(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime)
>> Post.first.attributes
=> {"id"=>1, "title"=>"OMG", "body"=>nil, "created_at"=>Wed, 26 Jun 2013 00:53:24 UTC +00:00, "updated_at"=>Wed, 26 Jun 2013 00:53:24 UTC +00:00}
>> Post.first.attributes[:title]
=> nil
>> Post.first.attributes['title']
=> "OMG"

To me it's just consistent to be all strings. Thanks @bogdan

@rafaelfranca thoughts?


This also makes sense to me. Consistency is a good thing.

However, do we have an idea on performance impact? I don't imagine it'll be much, but it would be good to know.


agree with @carlosantoniodasilva ... No need to make this with indifferent access.

We should close this IMO.

@bogdan bogdan deleted the bogdan:dirty-attributes-indifferent-hash branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2  activemodel/lib/active_model/dirty.rb
@@ -139,7 +139,7 @@ def previous_changes
# = 'robert'
# person.changed_attributes # => {"name" => "bob"}
def changed_attributes
- @changed_attributes ||= {}
+ @changed_attributes ||=
6 activemodel/test/cases/dirty_test.rb
@@ -125,4 +125,10 @@ def save
assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
assert_equal @model.name_was, "Otto"
+ test "changed_attributes" do
+ = "Otto"
+ assert_equal nil, @model.changed_attributes["name"]
+ assert_equal nil, @model.changed_attributes[:name]
+ end
Something went wrong with that request. Please try again.