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
Conversation
@@ -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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Post.select(:title)
?
Great work there @arthurnn! Could you please add a changelog entry and squash your commits ? |
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]
Fix regression on .select method
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
Support rails/rails#13886 by chainging select_rows arguments
This fixes a regression that is causing a bug in our lib/advanced_batches.rb The original commit message: Fix regression on .select method
Problem
That code wont work anymore on rails 4. And it used to work on rails 3.
Why
select
method, returns aActiveRecord::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]