Changeset 4007
- Timestamp:
- 03/21/06 16:33:22 (3 years ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations/has_many_through_association.rb (modified) (3 diffs)
- trunk/activerecord/test/associations_join_model_test.rb (modified) (1 diff)
- trunk/activerecord/test/fixtures/post.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r3997 r4007 1 1 *SVN* 2 2 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 3 17 * 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] 4 18 trunk/activerecord/lib/active_record/associations/has_many_through_association.rb
r3974 r4007 25 25 end 26 26 27 options[:select] = construct_select28 options[:from] = construct_from29 options[:joins] = construct_joins30 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? 31 31 32 32 merge_options_from_reflection!(options) … … 85 85 end 86 86 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}.*" 89 89 end 90 90 91 def construct_joins 91 def construct_joins(custom_joins = nil) 92 92 if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to 93 93 reflection_primary_key = @reflection.klass.primary_key … … 97 97 source_primary_key = @reflection.klass.primary_key 98 98 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}" % [ 101 101 @reflection.through_reflection.table_name, 102 102 @reflection.table_name, reflection_primary_key, trunk/activerecord/test/associations_join_model_test.rb
r4006 r4007 64 64 end 65 65 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 66 77 67 78 def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key trunk/activerecord/test/fixtures/post.rb
r3974 r4007 22 22 23 23 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 25 31 has_many :funky_tags, :through => :taggings, :class_name => 'Tag' 26 32 has_many :super_tags, :through => :taggings