diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 29b721a38ace4cf86ba2068266c4ac5c551e5e1a..8eb9ac1720b7ee1ee267cb919d50ff5df209fe35 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,21 @@ *SVN* +* Allow has_many :through to work on has_many associations (closes #3864) [sco@scottraymond.net] Example: + + class Firm < ActiveRecord::Base + has_many :clients + has_many :invoices, :through => :clients + end + + class Client < ActiveRecord::Base + belongs_to :firm + has_many :invoices + end + + class Invoice < ActiveRecord::Base + belongs_to :client + end + * Raise error when trying to select many polymorphic objects with has_many :through or :include (closes #4226) [josh@hasmanythrough.com] * Fixed has_many :through to include :conditions set on the :through association. closes #4020 [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 562d0e93fb9bbe829043bd9062f489dd6263756d..1baac52cdd8fabed314fe641a23a7d302ae5d6e6 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -26,6 +26,7 @@ def find(*args) options[:select] = construct_select options[:from] = construct_from + options[:joins] = construct_joins merge_options_from_reflection!(options) @@ -53,44 +54,57 @@ def find_target :select => construct_select, :conditions => construct_conditions, :from => construct_from, + :joins => construct_joins, :order => @reflection.options[:order], :limit => @reflection.options[:limit], - :joins => @reflection.options[:joins], :group => @reflection.options[:group] ) end - def construct_conditions - # Get the actual primary key of the belongs_to association that the reflection is going through - source_primary_key = @reflection.source_reflection.primary_key_name - - if @reflection.through_reflection.options[:as] - conditions = - "#{@reflection.table_name}.#{@reflection.klass.primary_key} = #{@reflection.through_reflection.table_name}.#{source_primary_key} " + - "AND #{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.options[:as]}_id = #{@owner.quoted_id} " + + def construct_conditions + conditions = if @reflection.through_reflection.options[:as] + "#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.options[:as]}_id = #{@owner.quoted_id} " + "AND #{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}" else - conditions = - "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{@reflection.through_reflection.table_name}.#{source_primary_key} " + - "AND #{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.primary_key_name} = #{@owner.quoted_id}" + case @reflection.source_reflection.macro + when :belongs_to, :has_many + "#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.primary_key_name} = #{@owner.quoted_id}" + else + raise ActiveRecordError, "Invalid source reflection macro :#{@reflection.source_reflection.macro} for has_many #{@reflection.name}, :through => #{@reflection.through_reflection.name}" + end end - conditions << " AND (#{sql_conditions})" if sql_conditions return conditions end def construct_from - "#{@owner.class.reflections[@reflection.options[:through]].table_name}, #{@reflection.table_name}" + @reflection.table_name end def construct_select selected = @reflection.options[:select] || "#{@reflection.table_name}.*" end + def construct_joins + 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 + else + 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]}" % [ + @owner.class.reflections[@reflection.options[:through]].table_name, + @reflection.table_name, reflection_primary_key, + @reflection.through_reflection.table_name, source_primary_key + ] + end + def construct_scope { - :find => { :from => construct_from, :conditions => construct_conditions }, + :find => { :from => construct_from, :conditions => construct_conditions, :joins => construct_joins }, :create => { @reflection.primary_key_name => @owner.id } } end @@ -115,9 +129,14 @@ def construct_sql end end - def sql_conditions - @conditions ||= interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions] + def conditions + @conditions ||= [ + (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]), + (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions]) + ].compact.collect { |condition| "(#{condition})" }.join(' AND ') unless (!@reflection.options[:conditions] && !@reflection.through_reflection.options[:conditions]) end + + alias_method :sql_conditions, :conditions end end end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 3011ba66257c935aa493d59fc568f97d143a747a..0d3333e0e14fee1675cc4726eef975692931c2e2 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -156,7 +156,9 @@ def source_reflection_name # def source_reflection return nil unless through_reflection - @source_reflection ||= through_reflection.klass.reflect_on_association(source_reflection_name) + @source_reflection ||= \ + through_reflection.klass.reflect_on_association(source_reflection_name) || # has_many :through a :belongs_to + through_reflection.klass.reflect_on_association(name) # has_many :through a :has_many end def check_validity! @@ -183,7 +185,7 @@ def name_to_class_name(name) if options[:class_name] options[:class_name] elsif through_reflection # get the class_name of the belongs_to association of the through reflection - through_reflection.klass.reflect_on_association(name.to_s.singularize.to_sym).class_name + source_reflection.class_name else class_name = name.to_s.camelize class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro) diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index f53a7669f4d50c6dc093d8f93255d44f06eaf9de..4b0f4e4c334841754ec780f7ed23eadbb04ba493 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -230,6 +230,22 @@ def test_has_many_polymorphic end end + def test_has_many_through_has_many_find_all + assert_equal comments(:greetings), authors(:david).comments.find(:all).first + end + + def test_has_many_through_has_many_find_first + assert_equal comments(:greetings), authors(:david).comments.find(:first) + end + + def test_has_many_through_has_many_find_conditions + assert_equal comments(:does_it_hurt), authors(:david).comments.find(:first, :conditions => "comments.type='SpecialComment'", :order => 'comments.id') + end + + def test_has_many_through_has_many_find_by_id + assert_equal comments(:more_greetings), authors(:david).comments.find(2) + end + private # create dynamic Post models to allow different dependency options def find_post_with_dependency(post_id, association, association_name, dependency) diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb index 06b0aaa1aeb630f9317efc4043673ab1e70bc455..1a7aa6e3889a0afd5ddde1aee049c8c58ed4602d 100644 --- a/activerecord/test/fixtures/author.rb +++ b/activerecord/test/fixtures/author.rb @@ -3,6 +3,7 @@ class Author < ActiveRecord::Base has_many :posts_with_comments, :include => :comments, :class_name => "Post" has_many :posts_with_categories, :include => :categories, :class_name => "Post" has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" + has_many :comments, :through => :posts has_many :special_posts, :class_name => "Post" has_many :hello_posts, :class_name => "Post", :conditions=>"\#{aliased_table_name}.body = 'hello'"