From a1bc64e9b49a58e2614e251ba710ab0abc879c92 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 29 Jul 2010 03:28:06 +0900 Subject: [PATCH] Compare not only by attribute_name but by attribute_name and table_name when compiling scope_for_create --- activerecord/lib/active_record/relation.rb | 2 +- activerecord/test/cases/method_scoping_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 86a210d..c48ded7 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -317,7 +317,7 @@ module ActiveRecord def scope_for_create @scope_for_create ||= begin @create_with_value || @where_values.inject({}) do |hash, where| - if where.is_a?(Arel::Predicates::Equality) + if where.is_a?(Arel::Predicates::Equality) && (where.operand1.relation == self.arel_table) hash[where.operand1.name] = where.operand2.respond_to?(:value) ? where.operand2.value : where.operand2 end hash diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 774b50e..d456b00 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -219,6 +219,12 @@ class MethodScopingTest < ActiveRecord::TestCase assert Post.find(1).comments.include?(new_comment) end + def test_scoped_create_with_join_and_merge + (Comment.where(:body => "but Who's Buying?").joins(:post) & Post.where(:body => 'Peace Sells...')).with_scope do + assert_equal({:body => "but Who's Buying?"}, Comment.scoped.scope_for_create) + end + end + def test_immutable_scope options = { :conditions => "name = 'David'" } Developer.send(:with_scope, :find => options) do -- 1.7.2