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???

Ruby on Rails member

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.

Ruby on Rails member

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.

Ruby on Rails member

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 May 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment