diff options
author | Rick Olson <technoweenie@gmail.com> | 2007-03-13 05:23:18 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2007-03-13 05:23:18 +0000 |
commit | e3dab67c44ed047cc91ccfcc17c5ee2bb9997be3 (patch) | |
tree | 5acc906178f850d70706a24c5ca47a3acdd43097 /activerecord/lib/active_record/associations | |
parent | 2823a56f14edac7e9012484d375f23c89405d725 (diff) | |
download | rails-e3dab67c44ed047cc91ccfcc17c5ee2bb9997be3.tar.gz rails-e3dab67c44ed047cc91ccfcc17c5ee2bb9997be3.tar.bz2 rails-e3dab67c44ed047cc91ccfcc17c5ee2bb9997be3.zip |
Allow a polymorphic :source for has_many :through associations. Closes #7143 [protocool]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6408 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 12 |
1 files changed, 11 insertions, 1 deletions
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 26559d523a..a8ad52bef9 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -138,7 +138,11 @@ module ActiveRecord # Construct attributes for :through pointing to owner and associate. def construct_join_attributes(associate) - construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.primary_key_name => associate.id) + returning construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.primary_key_name => associate.id) do |join_attributes| + if @reflection.options[:source_type] + join_attributes.merge!(@reflection.source_reflection.options[:foreign_type] => associate.class.base_class.name.to_s) + end + end end # Associate attributes pointing to owner, quoted. @@ -176,6 +180,12 @@ module ActiveRecord 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 + if @reflection.options[:source_type] + polymorphic_join = "AND %s.%s = %s" % [ + @reflection.through_reflection.table_name, "#{@reflection.source_reflection.options[:foreign_type]}", + @owner.class.quote_value(@reflection.options[:source_type]) + ] + end else reflection_primary_key = @reflection.source_reflection.primary_key_name source_primary_key = @reflection.klass.primary_key |