Skip to content

Commit

Permalink
Fix rewhere to allow overwriting association queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed May 4, 2020
1 parent ea4acf5 commit 7d4cc56
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
13 changes: 10 additions & 3 deletions activerecord/lib/active_record/relation/predicate_builder.rb
Expand Up @@ -84,10 +84,17 @@ def expand_from_hash(attributes)
end

klass ||= AssociationQueryValue
queries = klass.new(associated_table, value).queries.map do |query|
expand_from_hash(query).reduce(&:and)
queries = klass.new(associated_table, value).queries.map! do |query|
expand_from_hash(query)
end

if queries.one?
queries.first
else
queries.map! { |query| query.reduce(&:and) }
queries = queries.reduce { |result, query| Arel::Nodes::Or.new(result, query) }
Arel::Nodes::Grouping.new(queries)
end
queries.reduce(&:or)
elsif table.aggregated_with?(key)
mapping = table.reflect_on_aggregation(key).mapping
values = value.nil? ? [nil] : Array.wrap(value)
Expand Down
Expand Up @@ -110,7 +110,7 @@ def test_join_association_conditions_support_string_and_arel_expressions

def test_join_conditions_allow_nil_associations
authors = Author.includes(:essays).where(essays: { id: nil })
assert_equal 2, authors.count
assert_equal 1, authors.count
end

def test_find_with_implicit_inner_joins_without_select_does_not_imply_readonly
Expand Down
17 changes: 10 additions & 7 deletions activerecord/test/cases/relation/where_chain_test.rb
Expand Up @@ -3,18 +3,14 @@
require "cases/helper"
require "models/post"
require "models/author"
require "models/essay"
require "models/comment"
require "models/categorization"


module ActiveRecord
class WhereChainTest < ActiveRecord::TestCase
fixtures :posts, :authors

def setup
super
@name = "title"
end
fixtures :posts, :authors, :essays

def test_missing_with_association
assert posts(:authorless).author.blank?
Expand All @@ -40,7 +36,7 @@ def test_not_with_nil
end

def test_association_not_eq
expected = Comment.arel_table[@name].not_eq(Arel::Nodes::BindParam.new(1))
expected = Comment.arel_table["title"].not_eq(Arel::Nodes::BindParam.new(1))
relation = Post.joins(:comments).where.not(comments: { title: "hello" })
assert_equal(expected.to_sql, relation.where_clause.ast.to_sql)
end
Expand Down Expand Up @@ -93,6 +89,13 @@ def test_rewhere_with_one_overwriting_condition_and_one_unrelated
assert_equal expected.where_clause, relation.where_clause
end

def test_rewhere_with_polymorphic_association
relation = Essay.where(writer: authors(:david)).rewhere(writer_id: "Mary")
expected = Essay.where(writer: authors(:mary))

assert_equal expected, relation
end

def test_rewhere_with_range
relation = Post.where(comments_count: 1..3).rewhere(comments_count: 3..5)

Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/essays.yml
Expand Up @@ -4,3 +4,8 @@ david_modest_proposal:
writer_id: David
category_id: General
author_id: David

mary_stay_home:
name: Stay Home
writer_type: Author
writer_id: Mary

0 comments on commit 7d4cc56

Please sign in to comment.