Permalink
Browse files

Merge pull request #13896 from laurocaetano/dangerous_association_names

Associations now raises `ArgumentError` on name conflicts. Closes #13217.
  • Loading branch information...
2 parents 530cab3 + 4506dd2 commit 5df52961f7dad10af5d4341df5f8a62c4634b699 @carlosantoniodasilva carlosantoniodasilva committed Jan 31, 2014
View
@@ -1,3 +1,19 @@
+* Associations now raise `ArgumentError` on name conflicts.
+
+ Dangerous association names conflicts include instance or class methods already
+ defined by `ActiveRecord::Base`.
+
+ Example:
+
+ class Car < ActiveRecord::Base
+ has_many :errors
+ end
+ # Will raise ArgumentError.
+
+ Fixes #13217.
+
+ *Lauro Caetano*
+
* Fix regressions on `select_*` methods.
When `select_*` methods receive a `Relation` object, they should be able to get the arel/binds from it.
Also fix regressions on select_rows that was ignoring the binds.
@@ -26,6 +26,12 @@ class << self
attr_reader :name, :scope, :options
def self.build(model, name, scope, options, &block)
+ if model.dangerous_attribute_method?(name)
+ raise ArgumentError, "You tried to define an association named #{name} on the model #{model.name}, but " \
+ "this will conflict with a method #{name} already defined by Active Record. " \
+ "Please choose a different association name."
+ end
+
builder = create_builder model, name, scope, options, &block
reflection = builder.build(model)
define_accessors model, reflection
@@ -853,4 +853,14 @@ def test_reflect_the_most_recent_change
assert post.save
assert_equal post.author_id, author2.id
end
+
+ test 'dangerous association name raises ArgumentError' do
+ [:errors, 'errors', :save, 'save'].each do |name|
+ assert_raises(ArgumentError, "Association #{name} should not be allowed") do
+ Class.new(ActiveRecord::Base) do
+ belongs_to name
+ end
+ end
+ end
+ end
end
@@ -1820,4 +1820,14 @@ def test_collection_association_with_private_kernel_method
topic.approved_replies.create!
end
end
+
+ test 'dangerous association name raises ArgumentError' do
+ [:errors, 'errors', :save, 'save'].each do |name|
+ assert_raises(ArgumentError, "Association #{name} should not be allowed") do
+ Class.new(ActiveRecord::Base) do
+ has_many name
+ end
+ end
+ end
+ end
end
@@ -564,4 +564,14 @@ def test_has_one_relationship_cannot_have_a_counter_cache
end
end
end
+
+ test 'dangerous association name raises ArgumentError' do
+ [:errors, 'errors', :save, 'save'].each do |name|
+ assert_raises(ArgumentError, "Association #{name} should not be allowed") do
+ Class.new(ActiveRecord::Base) do
+ has_one name
+ end
+ end
+ end
+ end
end

0 comments on commit 5df5296

Please sign in to comment.