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

Changeset 8456

Show
Ignore:
Timestamp:
12/21/07 01:49:01 (5 months ago)
Author:
bitsweat
Message:

Eager belongs_to :include infers the foreign key from the association name rather than the class name. Closes #10517.

Files:

Legend:

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

    r8453 r8456  
    11*SVN* 
     2 
     3* Eager belongs_to :include infers the foreign key from the association name rather than the class name.  #10517 [Jonathan Viney] 
    24 
    35* SQLite: fix rename_ and remove_column for columns with unique indexes.  #10576 [Brandon Keepers] 
  • trunk/activerecord/lib/active_record/associations.rb

    r8305 r8456  
    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 
     
    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 
  • trunk/activerecord/test/associations/eager_test.rb

    r8315 r8456  
    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') 
  • trunk/activerecord/test/fixtures/author.rb

    r8376 r8456  
    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