diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 7949a1736d544..510b1c1f62066 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,19 @@ *SVN* +* Allow overriding of find parameters in scoped has_many :through calls [Rick Olson] + + In this example, :include => false disables the default eager association from loading. :select changes the standard + select clause. :joins specifies a join that is added to the end of the has_many :through query. + + class Post < ActiveRecord::Base + has_many :tags, :through => :taggings, :include => :tagging do + def add_joins_and_select + find :all, :select => 'tags.*, authors.id as author_id', :include => false, + :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id' + end + end + end + * 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] * Don't classify the has_one class when eager loading, it is already singular. Add tests. (closes #4117) [jonathan@bluewire.net.nz] diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 4e0b6f1c98464..ae34885480044 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -24,10 +24,10 @@ def find(*args) options[:order] = @reflection.options[:order] end - options[:select] = construct_select - options[:from] = construct_from - options[:joins] = construct_joins - options[:include] ||= @reflection.source_reflection.options[:include] + options[:select] = construct_select(options[:select]) + options[:from] ||= construct_from + options[:joins] = construct_joins(options[:joins]) + options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil? merge_options_from_reflection!(options) @@ -84,11 +84,11 @@ def construct_from @reflection.table_name end - def construct_select - selected = @reflection.options[:select] || "#{@reflection.table_name}.*" + def construct_select(custom_select = nil) + selected = custom_select || @reflection.options[:select] || "#{@reflection.table_name}.*" end - def construct_joins + def construct_joins(custom_joins = nil) if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to reflection_primary_key = @reflection.klass.primary_key source_primary_key = @reflection.source_reflection.primary_key_name @@ -96,8 +96,8 @@ def construct_joins reflection_primary_key = @reflection.source_reflection.primary_key_name source_primary_key = @reflection.klass.primary_key end - - "INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]}" % [ + + "INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]} #{custom_joins}" % [ @reflection.through_reflection.table_name, @reflection.table_name, reflection_primary_key, @reflection.through_reflection.table_name, source_primary_key diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index bbe88cfbd54b7..fc7a16cc8d326 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -63,6 +63,17 @@ def test_polymorphic_has_many_going_through_join_model_with_include_on_source_re end end + def test_polymorphic_has_many_going_through_join_model_with_disabled_include + assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first + assert_queries 1 do + tag.tagging + end + end + + def test_polymorphic_has_many_going_through_join_model_with_custom_select_and_joins + assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first + tag.author_id + end def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key assert_equal tags(:misc), taggings(:welcome_general).super_tag diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb index d3fa126b88d57..14b1c18117294 100644 --- a/activerecord/test/fixtures/post.rb +++ b/activerecord/test/fixtures/post.rb @@ -21,7 +21,13 @@ def find_most_recent has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id' has_many :taggings, :as => :taggable - has_many :tags, :through => :taggings, :include => :tagging + has_many :tags, :through => :taggings, :include => :tagging do + def add_joins_and_select + find :all, :select => 'tags.*, authors.id as author_id', :include => false, + :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id' + end + end + has_many :funky_tags, :through => :taggings, :class_name => 'Tag' has_many :super_tags, :through => :taggings has_one :tagging, :as => :taggable