Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #12011 from jetthoughts/11963_fix_join_with_associ…

…ation_scope

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

Conflicts:
	activerecord/CHANGELOG.md

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
commit c8eb8235bb777fda0a4e0ff0bc8ed45922339bca 1 parent 0dfb51b
Rafael Mendonça França rafaelfranca authored
8 activerecord/CHANGELOG.md
View
@@ -1,5 +1,13 @@
## unreleased ##
+* Fix: joins association, with defined in the scope block constraints by using several
+ where constraints and at least of them is not `Arel::Nodes::Equality`,
+ generates invalid SQL expression.
+
+ Fixes: #11963
+
+ *Paul Nikitochkin*
+
* Make possible to run SQLite rake tasks without the `Rails` constant defined.
*Damien Mathieu*
11 activerecord/lib/active_record/relation/query_methods.rb
View
@@ -875,14 +875,13 @@ def custom_join_ast(table, joins)
end
def collapse_wheres(arel, wheres)
- equalities = wheres.grep(Arel::Nodes::Equality)
-
- arel.where(Arel::Nodes::And.new(equalities)) unless equalities.empty?
-
- (wheres - equalities).each do |where|
+ predicates = wheres.map do |where|
+ next where if ::Arel::Nodes::Equality === where
where = Arel.sql(where) if String === where
- arel.where(Arel::Nodes::Grouping.new(where))
+ Arel::Nodes::Grouping.new(where)
end
+
+ arel.where(Arel::Nodes::And.new(predicates)) if predicates.present?
end
def build_where(opts, other = [])
5 activerecord/test/cases/associations/inner_join_association_test.rb
View
@@ -41,6 +41,11 @@ def test_join_conditions_added_to_join_clause
assert_no_match(/WHERE/i, sql)
end
+ def test_join_association_conditions_support_string_and_arel_expressions
+ assert_equal 0, Author.joins(:welcome_posts_with_comment).count
+ assert_equal 1, Author.joins(:welcome_posts_with_comments).count
+ end
+
def test_join_conditions_allow_nil_associations
authors = Author.includes(:essays).where(:essays => {:id => nil})
assert_equal 2, authors.count
7 activerecord/test/models/author.rb
View
@@ -31,6 +31,13 @@ def ratings
has_many :thinking_posts, -> { where(:title => 'So I was thinking') }, :dependent => :delete_all, :class_name => 'Post'
has_many :welcome_posts, -> { where(:title => 'Welcome to the weblog') }, :class_name => 'Post'
+ has_many :welcome_posts_with_comment,
+ -> { where(title: 'Welcome to the weblog').where('comments_count = ?', 1) },
+ class_name: 'Post'
+ has_many :welcome_posts_with_comments,
+ -> { where(title: 'Welcome to the weblog').where(Post.arel_table[:comments_count].gt(0)) },
+ class_name: 'Post'
+
has_many :comments_desc, -> { order('comments.id DESC') }, :through => :posts, :source => :comments
has_many :limited_comments, -> { limit(1) }, :through => :posts, :source => :comments
has_many :funky_comments, :through => :posts, :source => :comments
Please sign in to comment.
Something went wrong with that request. Please try again.