From f6379bfe154087bf93e1546bb7626941c77e9da7 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 21 Dec 2007 01:49:01 +0000 Subject: Eager belongs_to :include infers the foreign key from the association name rather than the class name. Closes #10517. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8456 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/associations.rb | 6 +++--- activerecord/test/associations/eager_test.rb | 5 +++++ activerecord/test/fixtures/author.rb | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index fb90d76f44..450f3821e8 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Eager belongs_to :include infers the foreign key from the association name rather than the class name. #10517 [Jonathan Viney] + * SQLite: fix rename_ and remove_column for columns with unique indexes. #10576 [Brandon Keepers] * Ruby 1.9 compatibility. [Jeremy Kemper] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 434612fdc0..9c408779a0 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -772,8 +772,8 @@ module ActiveRecord # * :order - specify the order in which the associated objects are returned as an ORDER BY SQL fragment, # such as last_name, first_name DESC # * :foreign_key - specify the foreign key used for the association. By default this is guessed to be the name - # of the associated class in lower-case and +_id+ suffixed. So a +Person+ class that makes a +belongs_to+ association to a - # +Boss+ class will use +boss_id+ as the default +foreign_key+. + # of the association with an +_id+ suffix. So a class that defines a +belongs_to :person+ association will use +person_id+ as the default +foreign_key+. + # Similarly, +belongs_to :favorite_person, :class_name => "Person"+ will use a foreign key of +favorite_person_id+. # * :counter_cache - caches the number of belonging objects on the associate class through the use of +increment_counter+ # and +decrement_counter+. The counter cache is incremented when an object of this class is created and decremented when it's # destroyed. This requires that a column named #{table_name}_count (such as +comments_count+ for a belonging +Comment+ class) @@ -1707,7 +1707,7 @@ module ActiveRecord connection.quote_table_name(aliased_table_name), reflection.klass.primary_key, connection.quote_table_name(parent.aliased_table_name), - options[:foreign_key] || klass.to_s.foreign_key + options[:foreign_key] || reflection.primary_key_name ] else "" diff --git a/activerecord/test/associations/eager_test.rb b/activerecord/test/associations/eager_test.rb index 4fc2e62842..69b1584b63 100644 --- a/activerecord/test/associations/eager_test.rb +++ b/activerecord/test/associations/eager_test.rb @@ -115,6 +115,11 @@ class EagerAssociationTest < Test::Unit::TestCase assert_equal [2], posts.collect { |p| p.id } end + def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name + author_favorite = AuthorFavorite.find(:first, :include => :favorite_author) + assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author } + end + def test_eager_association_loading_with_explicit_join posts = Post.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id') assert_equal 1, posts.length diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb index 1503e17f87..538ed7ac7f 100644 --- a/activerecord/test/fixtures/author.rb +++ b/activerecord/test/fixtures/author.rb @@ -105,5 +105,5 @@ end class AuthorFavorite < ActiveRecord::Base belongs_to :author - belongs_to :favorite_author, :class_name => "Author", :foreign_key => 'favorite_author_id' + belongs_to :favorite_author, :class_name => "Author" end -- cgit v1.2.3