diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index c0815198c20685d2e29e6803715224253c331390..5a1419415b6b88295c8f2151109038445a3285e0 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add deprecation warning for inferred foreign key. #6029 [Josh Susser] + * Fixed the Ruby/MySQL adapter we ship with Active Record to work with the new authentication handshake that was introduced in MySQL 4.1, along with the other protocol changes made at that time #5723 [jimw@mysql.com] * Deprecation: use :dependent => :delete_all rather than :exclusively_dependent => true. #6024 [Josh Susser] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 0771f262182834c6f887e594f9de6fc3add987d0..33fd8afee239dd17aec2eb6a4f5ac63f4adeb9ae 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -655,6 +655,12 @@ def has_one(association_id, options = {}) # :conditions => 'discounts > #{payments_count}' # belongs_to :attachable, :polymorphic => true def belongs_to(association_id, options = {}) + if options.include?(:class_name) && !options.include?(:foreign_key) + ::ActiveSupport::Deprecation.warn( + "The inferred foreign_key name will change in Rails 2.0 to use the association name instead of its class name when they differ. When using :class_name in belongs_to, use the :foreign_key option to explicitly set the key name to avoid problems in the transition.", + caller) + end + reflection = create_belongs_to_reflection(association_id, options) if reflection.options[:polymorphic] diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index b6ffda81059b6e8204cc3701a552295b8b9a2349..d7997beaa316af5165ed804615e06d2778e4e64b 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -350,6 +350,13 @@ def test_save_still_works_after_accessing_nil_has_one end end + def test_deprecated_inferred_foreign_key + assert_not_deprecated { Company.belongs_to :firm } + assert_not_deprecated { Company.belongs_to :client, :foreign_key => "firm_id" } + assert_not_deprecated { Company.belongs_to :firm, :class_name => "Firm", :foreign_key => "client_of" } + assert_deprecated("inferred foreign_key name") { Company.belongs_to :client, :class_name => "Firm" } + end + end