Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7188

Show
Ignore:
Timestamp:
07/16/07 20:21:36 (10 months ago)
Author:
rick
Message:

Change belongs_to so that the foreign_key assumption is taken from the association name, not the class name. Closes #8992 [hasmanyjosh]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r7187 r7188  
    11*SVN* 
     2 
     3* Change belongs_to so that the foreign_key assumption is taken from the association name, not the class name.  Closes #8992 [hasmanyjosh] 
     4 
     5  OLD 
     6    belongs_to :visitor, :class_name => 'User' # => inferred foreign_key is user_id 
     7     
     8  NEW 
     9    belongs_to :visitor, :class_name => 'User' # => inferred foreign_key is visitor_id 
    210 
    311* Remove spurious tests from deprecated_associations_test, most of these aren't deprecated, and are duplicated in associations_test.  Closes #8987 [lifofifo] 
  • trunk/activerecord/lib/active_record/associations.rb

    r7137 r7188  
    800800      #   belongs_to :attachable, :polymorphic => true 
    801801      def belongs_to(association_id, options = {}) 
    802         if options.include?(:class_name) && !options.include?(:foreign_key) 
    803           ::ActiveSupport::Deprecation.warn( 
    804           "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.", 
    805           caller) 
    806         end 
    807          
    808802        reflection = create_belongs_to_reflection(association_id, options) 
    809803         
  • trunk/activerecord/lib/active_record/reflection.rb

    r6408 r7188  
    208208        def derive_primary_key_name 
    209209          if macro == :belongs_to 
    210             class_name.foreign_key 
     210            "#{name}_id" 
    211211          elsif options[:as] 
    212212            "#{options[:as]}_id" 
  • trunk/activerecord/test/associations_test.rb

    r7137 r7188  
    410410  end 
    411411 
    412   def test_deprecated_inferred_foreign_key 
    413     assert_not_deprecated { Company.belongs_to :firm } 
    414     assert_not_deprecated { Company.belongs_to :client, :foreign_key => "firm_id" } 
    415     assert_not_deprecated { Company.belongs_to :firm, :class_name => "Firm", :foreign_key => "client_of" } 
    416     assert_deprecated("inferred foreign_key name") { Company.belongs_to :client, :class_name => "Firm" } 
    417   end 
    418  
    419412end 
    420413 
  • trunk/activerecord/test/reflection_test.rb

    r5923 r7188  
    112112  end 
    113113 
     114  def test_belongs_to_inferred_foreign_key_from_assoc_name 
     115    Company.belongs_to :foo 
     116    assert_equal "foo_id", Company.reflect_on_association(:foo).primary_key_name 
     117    Company.belongs_to :bar, :class_name => "Xyzzy" 
     118    assert_equal "bar_id", Company.reflect_on_association(:bar).primary_key_name 
     119    Company.belongs_to :baz, :class_name => "Xyzzy", :foreign_key => "xyzzy_id" 
     120    assert_equal "xyzzy_id", Company.reflect_on_association(:baz).primary_key_name 
     121  end 
     122 
    114123  def test_association_reflection_in_modules 
    115124    assert_reflection MyApplication::Business::Firm,