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

Prevent foreign_key_for? from evaluating all attributes #14140

Merged
merged 1 commit into from
Feb 21, 2014

Conversation

wkrsz
Copy link
Contributor

@wkrsz wkrsz commented Feb 21, 2014

Attributes should be evaluated lazily if possible (this prevents slow type-casting of date/time/timestamp columns when they are not used). attributes.has_key? caused all attributes to be evaluated just to set inverse association record instance.

@@ -232,7 +232,7 @@ def invertible_for?(record)

# Returns true if record contains the foreign_key
def foreign_key_for?(record)
record.attributes.has_key? reflection.foreign_key
record.attribute_names.include? reflection.foreign_key

Choose a reason for hiding this comment

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

Nice catch. You could actually make use of has_attribute? which is public api.

@wkrsz
Copy link
Contributor Author

wkrsz commented Feb 21, 2014

@carlosantoniodasilva thanks, changed!

@pftg
Copy link
Contributor

pftg commented Feb 21, 2014

May you add tests for this?

@dmathieu
Copy link
Contributor

@pftg: there's no feature change, nor bugfix. This is "only" a speed improvement.
There's no tests to be written.

@pftg
Copy link
Contributor

pftg commented Feb 21, 2014

@dmathieu thanks

@dmathieu
Copy link
Contributor

💖

carlosantoniodasilva added a commit that referenced this pull request Feb 21, 2014
Prevent foreign_key_for? from evaluating all attributes
@carlosantoniodasilva carlosantoniodasilva merged commit 467cb90 into rails:master Feb 21, 2014
@carlosantoniodasilva
Copy link
Member

Thanks.

carlosantoniodasilva added a commit that referenced this pull request Feb 21, 2014
Prevent foreign_key_for? from evaluating all attributes
carlosantoniodasilva added a commit that referenced this pull request Feb 21, 2014
Prevent foreign_key_for? from evaluating all attributes
@wkrsz wkrsz deleted the optimize_foreign_key_for branch February 21, 2014 19:13
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