aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-07-23 05:29:17 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-07-23 05:29:17 -0700
commitc4b93f57e1c29d6e1726d23d6ac2fdb75af9bdab (patch)
tree7fdbeebaac918d4cef96f2b294697e2c2cf850db /activerecord
parente0db277be0a57386d299fc0a231b585a612eb9ba (diff)
parentb289519f4ff7829271674d66d3196e1ee23056b6 (diff)
downloadrails-c4b93f57e1c29d6e1726d23d6ac2fdb75af9bdab.tar.gz
rails-c4b93f57e1c29d6e1726d23d6ac2fdb75af9bdab.tar.bz2
rails-c4b93f57e1c29d6e1726d23d6ac2fdb75af9bdab.zip
Merge pull request #11563 from 90yukke/fix-nomethoderror-on-non-attribute-equalities
Fix merge error when Equality LHS is non-attribute.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb5
-rw-r--r--activerecord/test/cases/relations_test.rb15
3 files changed, 23 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 608e10f866..343c6cac15 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.14 (Jul 22, 2013) ##
+* Fix merge error when Equality LHS is non-attribute.
+ Backport of #7380.
+
+ *Karmes Alexander*
+
* Do not re-create destroyed association when saving the parent object.
Fixes #11450.
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 90f2ac3cde..b734fc5c51 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -32,11 +32,12 @@ module ActiveRecord
merged_wheres = @where_values + r.where_values
unless @where_values.empty?
- # Remove duplicates, last one wins.
+ # Remove duplicate ARel attributes. Last one wins.
seen = Hash.new { |h,table| h[table] = {} }
merged_wheres = merged_wheres.reverse.reject { |w|
nuke = false
- if w.respond_to?(:operator) && w.operator == :==
+ if w.respond_to?(:operator) && w.operator == :== &&
+ w.left.respond_to?(:relation)
name = w.left.name
table = w.left.relation.name
nuke = seen[table][name]
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index f14eee2eb8..39632075e8 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -668,6 +668,21 @@ class RelationTest < ActiveRecord::TestCase
assert_equal [developers(:poor_jamis)], dev_with_count.to_a
end
+ def test_relation_merging_with_arel_equalities_keeps_last_equality
+ devs = Developer.where(Developer.arel_table[:salary].eq(80000)).merge(
+ Developer.where(Developer.arel_table[:salary].eq(9000))
+ )
+ assert_equal [developers(:poor_jamis)], devs.to_a
+ end
+
+ def test_relation_merging_with_arel_equalities_with_a_non_attribute_left_hand_ignores_non_attributes_when_discarding_equalities
+ salary_attr = Developer.arel_table[:salary]
+ devs = Developer.where(salary_attr.eq(80000)).merge(
+ Developer.where(salary_attr.eq(9000)).where(Arel::Nodes::NamedFunction.new('abs', [salary_attr]).eq(9000))
+ )
+ assert_equal [developers(:poor_jamis)], devs.to_a
+ end
+
def test_relation_merging_with_eager_load
relations = []
relations << Post.order('comments.id DESC').merge(Post.eager_load(:last_comment)).merge(Post.scoped)