Ticket #10517: belongs_to_with_inferred_foreign_key_and_docs.patch
| File belongs_to_with_inferred_foreign_key_and_docs.patch, 3.1 kB (added by jonathan_viney, 9 months ago) |
|---|
-
test/associations/eager_test.rb
old new 115 115 assert_equal [2], posts.collect { |p| p.id } 116 116 end 117 117 118 def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name 119 author_favorite = AuthorFavorite.find(:first, :include => :favorite_author) 120 assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author } 121 end 122 118 123 def test_eager_association_loading_with_explicit_join 119 124 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') 120 125 assert_equal 1, posts.length -
test/fixtures/author.rb
old new 105 105 106 106 class AuthorFavorite < ActiveRecord::Base 107 107 belongs_to :author 108 belongs_to :favorite_author, :class_name => "Author" , :foreign_key => 'favorite_author_id'108 belongs_to :favorite_author, :class_name => "Author" 109 109 end -
lib/active_record/associations.rb
old new 772 772 # * <tt>:order</tt> - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment, 773 773 # such as <tt>last_name, first_name DESC</tt> 774 774 # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name 775 # of the associat ed class in lower-case and +_id+ suffixed. So a +Person+ class that makes a +belongs_to+ association to a776 # +Boss+ class will use +boss_id+ as the default +foreign_key+.775 # 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+. 776 # Similarly, +belongs_to :favorite_person, :class_name => "Person"+ will use a foreign key of +favorite_person_id+. 777 777 # * <tt>:counter_cache</tt> - caches the number of belonging objects on the associate class through the use of +increment_counter+ 778 778 # and +decrement_counter+. The counter cache is incremented when an object of this class is created and decremented when it's 779 779 # destroyed. This requires that a column named <tt>#{table_name}_count</tt> (such as +comments_count+ for a belonging +Comment+ class) … … 1707 1707 connection.quote_table_name(aliased_table_name), 1708 1708 reflection.klass.primary_key, 1709 1709 connection.quote_table_name(parent.aliased_table_name), 1710 options[:foreign_key] || klass.to_s.foreign_key1710 options[:foreign_key] || reflection.primary_key_name 1711 1711 ] 1712 1712 else 1713 1713 ""