From ee8c006dac7d76f79fb43df97a4ab8a2ae87b8b2 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 22 Jan 2010 20:10:29 +0530 Subject: [PATCH] Allow calling class methods on a Relation --- activerecord/lib/active_record/relation.rb | 2 ++ activerecord/test/cases/named_scope_test.rb | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 04b85119cb..fa99e3f891 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -301,6 +301,8 @@ def eager_loading? def method_missing(method, *args, &block) if Array.method_defined?(method) to_a.send(method, *args, &block) + elsif @klass.respond_to?(method) + @klass.send(:with_scope, self) { @klass.send(method, *args, &block) } elsif arel.respond_to?(method) arel.send(method, *args, &block) elsif match = DynamicFinderMatch.match(method) diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 2c34ab787d..894d96346e 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -380,6 +380,15 @@ def test_deprecated_named_scope_method assert_deprecated('named_scope has been deprecated') { Topic.named_scope :deprecated_named_scope } end + def test_named_scopes_on_relations + # Topic.replied + approved_topics = Topic.scoped.approved.order('id DESC') + assert_equal topics(:fourth), approved_topics.first + + replied_approved_topics = approved_topics.replied + assert_equal topics(:third), replied_approved_topics.first + end + def test_index_on_named_scope approved = Topic.approved.order('id ASC') assert_equal topics(:second), approved[0] -- GitLab