diff --git a/activerecord/lib/arel.rb b/activerecord/lib/arel.rb index 75333d49f6d8912318a2b724311aa4241d46f09a..6263ef42adb3360c1d55ada019605f65a46c10ff 100644 --- a/activerecord/lib/arel.rb +++ b/activerecord/lib/arel.rb @@ -53,7 +53,7 @@ def self.fetch_attribute(value, &block) # :nodoc: Arel::Nodes::GreaterThan, Arel::Nodes::GreaterThanOrEqual if value.left.is_a?(Arel::Attributes::Attribute) yield value.left - else + elsif value.right.is_a?(Arel::Attributes::Attribute) yield value.right end when Arel::Nodes::Or diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 687e98fe0d7dfc12a77b67d1fd2b161f43d227a0..6d22c0be283567b29e5a585e3b817e4c102f5542 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -2050,6 +2050,18 @@ def test_unscope_specific_where_value assert_equal 1, posts.unscope(where: :body).count end + def test_unscope_with_arel_sql + posts = Post.where(Arel.sql("'Welcome to the weblog'").eq(Post.arel_attribute(:title))) + + assert_equal 1, posts.count + assert_equal Post.count, posts.unscope(where: :title).count + + posts = Post.where(Arel.sql("posts.title").eq("Welcome to the weblog")) + + assert_equal 1, posts.count + assert_equal 1, posts.unscope(where: :title).count + end + def test_unscope_grouped_where posts = Post.where( title: ["Welcome to the weblog", "So I was thinking", nil]