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

Fix regression on .select method #13886

Merged
merged 1 commit into from Jan 30, 2014
Merged

Conversation

arthurnn
Copy link
Member

Problem

query = author.posts.select(:title)
connection.select_one(query)

That code wont work anymore on rails 4. And it used to work on rails 3.

Why

select method, returns a ActiveRecord::AssociationRelation and the connection is not expecting that object.

Solution

If the argument is a ActiveRecord::AssociationRelation, get the arel and binds from it.

review @rafaelfranca @dmathieu

See this example that fails on AR 4 and not on 3:
https://gist.github.com/arthurnn/8701203

[fixes #7538]
[fixes #12017]
[related #13731]

@@ -20,6 +20,14 @@ def to_sql(arel, binds = [])

# Returns an ActiveRecord::Result instance.
def select_all(arel, name = nil, binds = [])
if arel.is_a?(ActiveRecord::AssociationRelation)
Copy link
Member

Choose a reason for hiding this comment

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

Should not we check for Relation only?

Copy link
Member Author

Choose a reason for hiding this comment

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

good point... my tests cases were always returning a AssociationRelation. I can change that.. any test test case in mind that I can create a Relation and pass to this method?

Copy link
Member

Choose a reason for hiding this comment

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

Post.select(:title)?

@robin850
Copy link
Member

Great work there @arthurnn! Could you please add a changelog entry and squash your commits ?

@arthurnn
Copy link
Member Author

Sure thing @robin850 ... 💚

@@ -1,3 +1,11 @@
* Fix regressions on `select_*` methods.
When `select_*` methods receive a `Relation` object, they should be able to get the arel/binds from it.
Also this fixes regressions on select_rows ignoring the binds.
Copy link
Member

Choose a reason for hiding this comment

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

To make it consistent with the first sentence, we could simply put Also fix regressions on ..., what do you think ? Also, I think that the indentation is weird on this line.

Copy link
Member Author

Choose a reason for hiding this comment

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

make sense... will fix it...
emacs is messing my indentation =)

This was a common pattern:
```
query = author.posts.select(:title)
connection.select_one(query)
```

However `.select` returns a ActiveRecord::AssociationRelation, which has
the bind information, so we can use that to get the right sql query.

Also fix select_rows on postgress and sqlite3 that were not using the binds

[fixes rails#7538]
[fixes rails#12017]
[related rails#13731]
[related rails#12056]
rafaelfranca added a commit that referenced this pull request Jan 30, 2014
@rafaelfranca rafaelfranca merged commit 27aedee into rails:master Jan 30, 2014
rafaelfranca added a commit that referenced this pull request Jan 30, 2014
Fix regression on .select method
Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
	activerecord/test/cases/adapter_test.rb
@arthurnn arthurnn deleted the fix_relation_arel branch January 30, 2014 20:31
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Feb 10, 2014
yahonda added a commit to rsim/oracle-enhanced that referenced this pull request Feb 10, 2014
Support rails/rails#13886 by chainging select_rows arguments
tdooner pushed a commit to causes/rails that referenced this pull request Feb 21, 2014
This fixes a regression that is causing a bug in our
lib/advanced_batches.rb

The original commit message: Fix regression on .select method
arthurnn added a commit to arthurnn/rails that referenced this pull request Mar 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants