From 1cdec77c0d76a52a83fa72d3a6a0ffeac177946b Mon Sep 17 00:00:00 2001 From: Andrea Campi Date: Sun, 10 Oct 2010 23:56:18 +0200 Subject: [PATCH] Raise ActiveRecord::DangerousAttributeError on attempts to define an association with the same name as an ActiveRecord method. [#5772 state:resolved] --- activerecord/lib/active_record/associations.rb | 1 + activerecord/test/cases/attribute_methods_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 565ebf8..8d49598 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1442,6 +1442,7 @@ module ActiveRecord end def association_accessor_methods(reflection, association_proxy_class) + return if instance_method_already_implemented?(reflection.name) redefine_method(reflection.name) do |*params| force_reload = params.first unless params.empty? association = association_instance_get(reflection.name) diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 1750bf0..fa09c55 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -406,6 +406,15 @@ class AttributeMethodsTest < ActiveRecord::TestCase end end + def test_raises_dangerous_attribute_error_when_defining_activerecord_method_as_association + %w(save create_or_update update).each do |method| + klass = Class.new ActiveRecord::Base + assert_raise ActiveRecord::DangerousAttributeError do + klass.class_eval "belongs_to :#{method}" + end + end + end + def test_only_time_related_columns_are_meant_to_be_cached_by_default expected = %w(datetime timestamp time date).sort assert_equal expected, ActiveRecord::Base.attribute_types_cached_by_default.map(&:to_s).sort -- 1.7.2