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

Collapse where constraints to the Arel::Nodes::And node #12011

Merged

Conversation

pftg
Copy link
Contributor

@pftg pftg commented Aug 24, 2013

In order to remove duplication with joining where constraints by AND on traverse arel.constraints,
refactored collapse_wheres to add to arel.wheres on build_arel only one node Arel::Nodes::And.

This refactoring remove duplication to which fixed #11963:

--- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -122,7 +122,7 @@ module ActiveRecord
             end

             if rel && !rel.arel.constraints.empty?
-              constraint = constraint.and rel.arel.constraints
+              constraint = rel.arel.constraints.inject(constraint) {|constraint, c| constraint.and c}
             end

             joins << join(table, constraint)

because Arel::Nodes::Node#and expected that argument (in our case rel.arel.constraints) is not Array, in that way Arel::Visitors::ToSql on visiting Array returns , instead of AND.

@pftg
Copy link
Contributor Author

pftg commented Aug 24, 2013

/cc @tenderlove, @senny, @rafaelfranca

@pftg
Copy link
Contributor Author

pftg commented Aug 24, 2013

Build should be restarted because of minitest problems.

@senny
Copy link
Member

senny commented Aug 24, 2013

@pftg done.

@pftg
Copy link
Contributor Author

pftg commented Aug 24, 2013

Thanks @senny

@pftg
Copy link
Contributor Author

pftg commented Sep 6, 2013

@senny, also may you fast review this. Travis failed because of Segmentation fault

@pftg
Copy link
Contributor Author

pftg commented Sep 10, 2013

Rebased and tests are passed.

@@ -41,6 +41,15 @@ def test_join_conditions_added_to_join_clause
assert_no_match(/WHERE/i, sql)
end

def test_join_association_conditions_added_to_join_clause
sql = Author.joins(:welcome_posts_with_comment).to_sql
assert_match(/AND \(comments_count = 1\)/i, sql)
Copy link
Member

Choose a reason for hiding this comment

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

Can we assert the result instead of the SQL? The SQL generation can change in the future and this test will broke.

Copy link
Member

Choose a reason for hiding this comment

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

This gist has very good test cases. Mind to reuse they? https://gist.github.com/iwiznia/6295952

In order to remove duplication with joining arel where constraints with
`AND`, all constraints on `build_arel` are collapsed into one head node: `Arel::Nodes::And`

Closes: rails#11963
@pftg
Copy link
Contributor Author

pftg commented Sep 13, 2013

@rafaelfranca thanks for feedback, I updated Changelog, and replaced sql assertions similar to gists examples by asserting results.

rafaelfranca added a commit that referenced this pull request Sep 16, 2013
…ation_scope

Collapse where constraints to the Arel::Nodes::And node

Conflicts:
	activerecord/CHANGELOG.md
@rafaelfranca rafaelfranca merged commit fbbb6c8 into rails:master Sep 16, 2013
rafaelfranca added a commit that referenced this pull request Sep 16, 2013
…ation_scope

Collapse where constraints to the Arel::Nodes::And node

Conflicts:
	activerecord/CHANGELOG.md

Conflicts:
	activerecord/CHANGELOG.md
@pftg pftg deleted the 11963_fix_join_with_association_scope branch September 21, 2013 06:15
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.

Joining an association with scope block failing
3 participants