diff options
author | Jon Leighton <j@jonathanleighton.com> | 2010-10-12 16:40:24 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2010-10-12 16:40:24 +0100 |
commit | 1777600e6e11e553ad97b7bc89e4b19e992eb3d3 (patch) | |
tree | 7bdf7c72cf9804d106487f132969e52683a35a3c /activerecord/lib/active_record/associations | |
parent | 7aea695815821df332913bae4b47714a525009a3 (diff) | |
download | rails-1777600e6e11e553ad97b7bc89e4b19e992eb3d3.tar.gz rails-1777600e6e11e553ad97b7bc89e4b19e992eb3d3.tar.bz2 rails-1777600e6e11e553ad97b7bc89e4b19e992eb3d3.zip |
Support has_one through assocs as the source association
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/through_association_scope.rb | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb index 8406f5fd20..81e29f047b 100644 --- a/activerecord/lib/active_record/associations/through_association_scope.rb +++ b/activerecord/lib/active_record/associations/through_association_scope.rb @@ -53,7 +53,7 @@ module ActiveRecord end def construct_joins(custom_joins = nil) - # puts @reflection.through_reflection_chain.map(&:inspect) + # p @reflection.through_reflection_chain "#{construct_through_joins} #{@reflection.options[:joins]} #{custom_joins}" end @@ -67,16 +67,27 @@ module ActiveRecord case when left.source_reflection.nil? - left_primary_key = left.primary_key_name - right_primary_key = right.klass.primary_key + # TODO: Perhaps need to pay attention to left.options[:primary_key] and + # left.options[:foreign_key] in places here - if left.options[:as] - polymorphic_join = "AND %s.%s = %s" % [ - table_aliases[left], "#{left.options[:as]}_type", - # TODO: Why right.klass.name? Rather than left.active_record.name? - # TODO: Also should maybe use the base_class (see related code in JoinAssociation) - @owner.class.quote_value(right.klass.name) - ] + case left.macro + when :belongs_to + left_primary_key = left.klass.primary_key + right_primary_key = right.primary_key_name + when :has_many, :has_one + left_primary_key = left.primary_key_name + right_primary_key = right.klass.primary_key + + if left.options[:as] + polymorphic_join = "AND %s.%s = %s" % [ + table_aliases[left], "#{left.options[:as]}_type", + # TODO: Why right.klass.name? Rather than left.active_record.name? + # TODO: Also should maybe use the base_class (see related code in JoinAssociation) + @owner.class.quote_value(right.klass.name) + ] + end + when :has_and_belongs_to_many + raise NotImplementedError end when left.source_reflection.macro == :belongs_to left_primary_key = left.klass.primary_key |