Skip to content

Commit

Permalink
Change belongs_to so that the foreign_key assumption is taken from th…
Browse files Browse the repository at this point in the history
…e association name, not the class name. Closes #8992 [hasmanyjosh]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7188 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Jul 16, 2007
1 parent ddb00f1 commit 4ef5af8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
8 changes: 8 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
*SVN*

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

OLD
belongs_to :visitor, :class_name => 'User' # => inferred foreign_key is user_id

NEW
belongs_to :visitor, :class_name => 'User' # => inferred foreign_key is visitor_id

* Remove spurious tests from deprecated_associations_test, most of these aren't deprecated, and are duplicated in associations_test. Closes #8987 [lifofifo]

* Make create! on a has_many :through association return the association object. Not the collection. Closes #8786 [lifofifo]
Expand Down
6 changes: 0 additions & 6 deletions activerecord/lib/active_record/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -799,12 +799,6 @@ 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]
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def derive_class_name

def derive_primary_key_name
if macro == :belongs_to
class_name.foreign_key
"#{name}_id"
elsif options[:as]
"#{options[:as]}_id"
else
Expand Down
7 changes: 0 additions & 7 deletions activerecord/test/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,6 @@ 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


Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ def test_has_one_reflection
assert_equal 'accounts', Firm.reflect_on_association(:account).table_name
end

def test_belongs_to_inferred_foreign_key_from_assoc_name
Company.belongs_to :foo
assert_equal "foo_id", Company.reflect_on_association(:foo).primary_key_name
Company.belongs_to :bar, :class_name => "Xyzzy"
assert_equal "bar_id", Company.reflect_on_association(:bar).primary_key_name
Company.belongs_to :baz, :class_name => "Xyzzy", :foreign_key => "xyzzy_id"
assert_equal "xyzzy_id", Company.reflect_on_association(:baz).primary_key_name
end

def test_association_reflection_in_modules
assert_reflection MyApplication::Business::Firm,
:clients_of_firm,
Expand Down

0 comments on commit 4ef5af8

Please sign in to comment.