aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/scoping/named.rb
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-01-24 18:28:05 -0800
committerGodfrey Chan <godfreykfc@gmail.com>2014-01-29 10:54:51 -0800
commit7e8e91c439c1a877f867cd7ba634f7297ccef04b (patch)
tree4d800773aa5f59b9293c52b3b46c44d27b3a4d5e /activerecord/lib/active_record/scoping/named.rb
parent9ed66648b59b160b43c83c349263e8cb97eaa088 (diff)
downloadrails-7e8e91c439c1a877f867cd7ba634f7297ccef04b.tar.gz
rails-7e8e91c439c1a877f867cd7ba634f7297ccef04b.tar.bz2
rails-7e8e91c439c1a877f867cd7ba634f7297ccef04b.zip
`scope` now raises on "dangerous" name conflicts
Similar to dangerous attribute methods, a scope name conflict is dangerous if it conflicts with an existing class method defined within `ActiveRecord::Base` but not its ancestors. See also #13389. *Godfrey Chan*, *Philippe Creux*
Diffstat (limited to 'activerecord/lib/active_record/scoping/named.rb')
-rw-r--r--activerecord/lib/active_record/scoping/named.rb6
1 files changed, 6 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index 2a5718f388..49cadb66d0 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -139,6 +139,12 @@ module ActiveRecord
# Article.published.featured.latest_article
# Article.featured.titles
def scope(name, body, &block)
+ 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 " \
+ "a class method with the same name."
+ end
+
extension = Module.new(&block) if block
singleton_class.send(:define_method, name) do |*args|