From dec06b7a24ae20c894f72fa15e389c896608b3e8 Mon Sep 17 00:00:00 2001 From: Pascal Friederich Date: Thu, 17 Jul 2008 03:42:52 +0200 Subject: [PATCH] HMT associations with conditions on the join table now create the join model with the value given in the permision. only works if the condition value is a String --- .../associations/has_many_through_association.rb | 19 +++++++++++++++---- 1 files changed, 15 insertions(+), 4 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 e1bfff5..d2c1e96 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -84,20 +84,31 @@ module ActiveRecord end # Construct attributes for associate pointing to owner. - def construct_owner_attributes(reflection) + def construct_owner_attributes(reflection, root_reflection = nil) if as = reflection.options[:as] - { "#{as}_id" => @owner.id, + scope_attributes = { "#{as}_id" => @owner.id, "#{as}_type" => @owner.class.base_class.name.to_s } else - { reflection.primary_key_name => @owner.id } + scope_attributes = { reflection.primary_key_name => @owner.id } end + + if !root_reflection.nil? && root_reflection.options[:conditions] + root_reflection.options[:conditions].each do |k, v| + if k.include?(".") + table_name, k = k.split(".", 2) + end + scope_attributes.merge!({k => v}) + + end + end + scope_attributes end # Construct attributes for :through pointing to owner and associate. def construct_join_attributes(associate) # TODO: revist this to allow it for deletion, supposing dependent option is supported raise ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection.new(@owner, @reflection) if @reflection.source_reflection.macro == :has_many - join_attributes = construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.primary_key_name => associate.id) + join_attributes = construct_owner_attributes(@reflection.through_reflection, @reflection).merge(@reflection.source_reflection.primary_key_name => associate.id) if @reflection.options[:source_type] join_attributes.merge!(@reflection.source_reflection.options[:foreign_type] => associate.class.base_class.name.to_s) end -- 1.5.3.7