aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-23 13:40:38 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-23 13:41:09 +0530
commit8ff2fb6f3aa6140f5a8bd018d5919a8a1e707cda (patch)
treeddffd8e35044c3c56336819d585e0896b1d3b457 /activerecord
parentc2e57c0bb02396cd7762d67f5dd8d6aa1e1814a9 (diff)
downloadrails-8ff2fb6f3aa6140f5a8bd018d5919a8a1e707cda.tar.gz
rails-8ff2fb6f3aa6140f5a8bd018d5919a8a1e707cda.tar.bz2
rails-8ff2fb6f3aa6140f5a8bd018d5919a8a1e707cda.zip
Make default_scope work with Relations
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/base.rb5
-rw-r--r--activerecord/test/cases/method_scoping_test.rb8
-rw-r--r--activerecord/test/models/post.rb5
3 files changed, 15 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index f1b2b3b979..12feef4849 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1236,7 +1236,7 @@ module ActiveRecord #:nodoc:
end
def construct_finder_arel(options = {}, scope = nil)
- relation = unscoped.apply_finder_options(options)
+ relation = options.is_a?(Hash) ? unscoped.apply_finder_options(options) : unscoped.merge(options)
relation = scope.merge(relation) if scope
relation
end
@@ -1450,7 +1450,8 @@ module ActiveRecord #:nodoc:
end
def scoped_methods #:nodoc:
- Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping.dup
+ key = :"#{self}_scoped_methods"
+ Thread.current[key] = Thread.current[key].presence || self.default_scoping.dup
end
def current_scoped_methods #:nodoc:
diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb
index fbd1adf088..1081aa40a9 100644
--- a/activerecord/test/cases/method_scoping_test.rb
+++ b/activerecord/test/cases/method_scoping_test.rb
@@ -588,7 +588,7 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
end
class DefaultScopingTest < ActiveRecord::TestCase
- fixtures :developers
+ fixtures :developers, :posts
def test_default_scope
expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary }
@@ -657,6 +657,12 @@ class DefaultScopingTest < ActiveRecord::TestCase
received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
assert_equal expected, received
end
+
+ def test_default_scope_using_relation
+ posts = PostWithComment.scoped
+ assert_equal 2, posts.count
+ assert_equal posts(:thinking), posts.first
+ end
end
=begin
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index f48b35486c..704313649a 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -100,3 +100,8 @@ end
class SubStiPost < StiPost
self.table_name = Post.table_name
end
+
+class PostWithComment < ActiveRecord::Base
+ self.table_name = 'posts'
+ default_scope where("posts.comments_count > 0").order("posts.comments_count ASC")
+end