diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 49cadb66d021e6bf00f1814af58fd0876ffd013a..04aeeeb4fa4bbe9a3934acd60843ec6a8fbac358 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -139,6 +139,9 @@ def scope_attributes? # :nodoc: # Article.published.featured.latest_article # Article.featured.titles def scope(name, body, &block) + raise ArgumentError, 'You need to suply a callable as scope body' unless + body.respond_to?(:call) + if dangerous_class_method?(name) raise ArgumentError, "You tried to define a scope named \"#{name}\" " \ "on the model \"#{self.name}\", but Active Record already defined " \ diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb index d3546bd4719794c6cdf383d4962c4df834e0221d..d8906b229960b9b3bc789843bd3be65211f229f4 100644 --- a/activerecord/test/cases/scoping/named_scoping_test.rb +++ b/activerecord/test/cases/scoping/named_scoping_test.rb @@ -132,6 +132,12 @@ def test_scopes_honor_current_scopes_from_when_defined assert_equal Post.ranked_by_comments.limit_by(5), Post.top(5) end + def test_scopes_body_is_a_callable + assert_raises(ArgumentError, 'You need to suply a callable as scope body') do + Post.class_eval { scope :containing_the_letter_z, where("body LIKE '%z%'") } + end + end + def test_active_records_have_scope_named__all__ assert !Topic.all.empty?