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

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)

Add documentation changes

  • test/associations/eager_test.rb

    old new  
    115115    assert_equal [2], posts.collect { |p| p.id } 
    116116  end 
    117117   
     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   
    118123  def test_eager_association_loading_with_explicit_join 
    119124    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') 
    120125    assert_equal 1, posts.length 
  • test/fixtures/author.rb

    old new  
    105105 
    106106class AuthorFavorite < ActiveRecord::Base 
    107107  belongs_to :author 
    108   belongs_to :favorite_author, :class_name => "Author", :foreign_key => 'favorite_author_id' 
     108  belongs_to :favorite_author, :class_name => "Author" 
    109109end 
  • lib/active_record/associations.rb

    old new  
    772772      # * <tt>:order</tt>       - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment, 
    773773      #   such as <tt>last_name, first_name DESC</tt> 
    774774      # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name 
    775       #   of the associated class in lower-case and +_id+ suffixed. So a +Person+ class that makes a +belongs_to+ association to a 
    776       #   +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+. 
    777777      # * <tt>:counter_cache</tt> - caches the number of belonging objects on the associate class through the use of +increment_counter+  
    778778      #   and +decrement_counter+. The counter cache is incremented when an object of this class is created and decremented when it's 
    779779      #   destroyed. This requires that a column named <tt>#{table_name}_count</tt> (such as +comments_count+ for a belonging +Comment+ class) 
     
    17071707                     connection.quote_table_name(aliased_table_name), 
    17081708                     reflection.klass.primary_key, 
    17091709                     connection.quote_table_name(parent.aliased_table_name), 
    1710                      options[:foreign_key] || klass.to_s.foreign_key 
     1710                     options[:foreign_key] || reflection.primary_key_name 
    17111711                    ] 
    17121712                else 
    17131713                  ""