From 980cbe05408a89492adc0bcade018bc1e87a055b Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Mon, 28 Jun 2010 17:17:56 -0400 Subject: [PATCH] with_exclusive_scope does not work properly if ARel is passed. It does work nicely if hash is passed. Blow up if user is attempting it pass ARel to with_exclusive_scope. [#3838 state:resolved] --- activerecord/lib/active_record/base.rb | 13 +++++++++++++ activerecord/test/cases/method_scoping_test.rb | 6 ++++++ activerecord/test/models/developer.rb | 8 +++++++- 3 files changed, 26 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index e7b5228..216386d 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1143,7 +1143,20 @@ module ActiveRecord #:nodoc: end # Works like with_scope, but discards any nested properties. + # TODO : this method should be deprecated in favor of standard query API def with_exclusive_scope(method_scoping = {}, &block) + if method_scoping.values.any? { |e| e.is_a?(ActiveRecord::Relation) } + msg =<<-MSG + ARel can not be used with_exclusive_scope. You can either specify hash style conditions to with_exclusive_scope like this: + User.with_exclusive_scope {:find => :conditions => {:active => true} } do + end + + Or you can use unscoped method instead of with_exclusive_scope like this: + User.unscoped.where(:active => true) do + end + MSG + raise ArgumentError.new(msg) + end with_scope(method_scoping, :overwrite, &block) end diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 6cd42ff..178562e 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -283,6 +283,12 @@ class NestedScopingTest < ActiveRecord::TestCase end end + def test_with_exclusive_scope_with_relation + assert_raise(ArgumentError) do + Developer.all_johns + end + end + def test_append_conditions Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index e35de3b..de68fd7 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -55,6 +55,12 @@ class Developer < ActiveRecord::Base def log=(message) audit_logs.build :message => message end + + def self.all_johns + self.with_exclusive_scope :find => where(:name => 'John') do + self.all + end + end end class AuditLog < ActiveRecord::Base @@ -103,4 +109,4 @@ end class PoorDeveloperCalledJamis < ActiveRecord::Base self.table_name = 'developers' default_scope :conditions => { :name => 'Jamis', :salary => 50000 } -end \ No newline at end of file +end -- 1.6.5.2