Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make reflection and aggregate_reflection caches work with string keys. #14675

Merged
merged 4 commits into from
Apr 10, 2014
Merged

Make reflection and aggregate_reflection caches work with string keys. #14675

merged 4 commits into from
Apr 10, 2014

Conversation

laurocaetano
Copy link
Contributor

Following @tenderlove's suggestion on #14668 (comment), now the reflection and the aggregate_reflection caches use String as keys. This is needed to avoid to_sym in some cases, such as did in #14668.

I think we won't have problems changing these caches, since they are only used in reflection.rb file.

Another point is the test. I think it is wrong, it should not use the reflection Hash, right?

cc/ @tenderlove

@tenderlove
Copy link
Member

It definitely shouldn't be touching the has directly like that. Do we have accessor functions for the hash?

Aaron Patterson
http://tenderlovemaking.com/
I'm on an iPhone so I apologize for top posting.

On Apr 9, 2014, at 6:21 PM, Lauro Caetano notifications@github.com wrote:

Following @tenderlove's suggestion on #14668 (comment), now the reflection and the aggregate_reflection caches use String as keys. This is needed to avoid to_sym in some cases, such as did in #14668.

I think we won't have problems changing these caches, since they are only used in reflection.rb file.

Another point is the test. I think it is wrong, it should not use the reflection Hash, right?

cc/ @tenderlove

You can merge this Pull Request by running

git pull https://github.com/laurocaetano/rails make_reflection_caches_works_with_string_keys
Or view, comment on, or merge it at:

#14675

Commit Summary

Make the reflections cache work with strings as its keys.
No need to call to_sym on reflection name, since the cache now works
Make the aggregate_reflections cache work with strings as its keys.
Make sure the reflection test is passing a String to the reflection
File Changes

M activerecord/lib/active_record/reflection.rb (12)
M activerecord/test/cases/reflection_test.rb (2)
Patch Links:

https://github.com/rails/rails/pull/14675.patch
https://github.com/rails/rails/pull/14675.diff

Reply to this email directly or view it on GitHub.

tenderlove added a commit that referenced this pull request Apr 10, 2014
…rks_with_string_keys

Make reflection and aggregate_reflection caches work with string keys.
@tenderlove tenderlove merged commit 82cb477 into rails:master Apr 10, 2014
@laurocaetano
Copy link
Contributor Author

It definitely shouldn't be touching the has directly like that. Do we have accessor functions for the hash?

Yes, they are using the class_attribute here https://github.com/rails/rails/blob/master/activerecord/lib/active_record/reflection.rb#L7-L8

end

def source_reflection_name # :nodoc:
return @source_reflection_name.to_sym if @source_reflection_name
return @source_reflection_name if @source_reflection_name

names = [name.to_s.singularize, name].collect { |n| n.to_sym }.uniq

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing that doesn't actually affect this piece where it looks for the real name, right? The "names" method is only used in an exception as far as I could see.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlosantoniodasilva yes, you are right. Thanks bro!

eileencodes added a commit to eileencodes/rails that referenced this pull request Jun 18, 2014
In PR rails#14675 reflection cache / associations were changed to accept
string as keys. The problem is this messed up the ability to accepts
symbols as keys. There were no tests to make sure both strings and symbols
could be accepts. This was brought to light when I attempted to fix rails#15671.
fragoulis added a commit to fragoulis/agile_serializer that referenced this pull request Sep 14, 2015
Update the code that uses the internal rails api to be compatible with rails 4.2.

> ActiveRecord::Base#reflections now returns a hash with string keys instead of symbol keys.

[PR] rails/rails#14675
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants