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

Changeset 4007

Show
Ignore:
Timestamp:
03/21/06 16:33:22 (3 years ago)
Author:
rick
Message:

Allow overriding of find parameters in scoped has_many :through calls [Rick Olson]

Files:

Legend:

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

    r3997 r4007  
    11*SVN* 
    22 
     3* Allow overriding of find parameters in scoped has_many :through calls [Rick Olson] 
     4 
     5  In this example, :include => false disables the default eager association from loading.  :select changes the standard 
     6  select clause.  :joins specifies a join that is added to the end of the has_many :through query. 
     7   
     8    class Post < ActiveRecord::Base 
     9      has_many :tags, :through => :taggings, :include => :tagging do 
     10        def add_joins_and_select 
     11          find :all, :select => 'tags.*, authors.id as author_id', :include => false, 
     12            :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id' 
     13        end 
     14      end 
     15    end 
     16     
    317* Fixed that schema changes while the database was open would break any connections to a SQLite database (now we reconnect if that error is throw) [DHH] 
    418 
  • trunk/activerecord/lib/active_record/associations/has_many_through_association.rb

    r3974 r4007  
    2525        end 
    2626         
    27         options[:select]    = construct_select 
    28         options[:from]      = construct_from 
    29         options[:joins]     = construct_joins 
    30         options[:include] ||= @reflection.source_reflection.options[:include] 
     27        options[:select]  = construct_select(options[:select]) 
     28        options[:from]  ||= construct_from 
     29        options[:joins]   = construct_joins(options[:joins]) 
     30        options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil? 
    3131         
    3232        merge_options_from_reflection!(options) 
     
    8585        end 
    8686         
    87         def construct_select 
    88           selected = @reflection.options[:select] || "#{@reflection.table_name}.*"           
     87        def construct_select(custom_select = nil) 
     88          selected = custom_select || @reflection.options[:select] || "#{@reflection.table_name}.*"           
    8989        end 
    9090         
    91         def construct_joins 
     91        def construct_joins(custom_joins = nil) 
    9292          if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to 
    9393            reflection_primary_key = @reflection.klass.primary_key 
     
    9797            source_primary_key     = @reflection.klass.primary_key 
    9898          end 
    99            
    100           "INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]}" % [ 
     99 
     100          "INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]} #{custom_joins}" % [ 
    101101            @reflection.through_reflection.table_name, 
    102102            @reflection.table_name, reflection_primary_key, 
  • trunk/activerecord/test/associations_join_model_test.rb

    r4006 r4007  
    6464  end 
    6565 
     66  def test_polymorphic_has_many_going_through_join_model_with_disabled_include 
     67    assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first 
     68    assert_queries 1 do 
     69      tag.tagging 
     70    end 
     71  end 
     72 
     73  def test_polymorphic_has_many_going_through_join_model_with_custom_select_and_joins 
     74    assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first 
     75    tag.author_id 
     76  end 
    6677 
    6778  def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key 
  • trunk/activerecord/test/fixtures/post.rb

    r3974 r4007  
    2222 
    2323  has_many :taggings, :as => :taggable 
    24   has_many :tags, :through => :taggings, :include => :tagging 
     24  has_many :tags, :through => :taggings, :include => :tagging do 
     25    def add_joins_and_select 
     26      find :all, :select => 'tags.*, authors.id as author_id', :include => false, 
     27        :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id' 
     28    end 
     29  end 
     30   
    2531  has_many :funky_tags, :through => :taggings, :class_name => 'Tag' 
    2632  has_many :super_tags, :through => :taggings