From 1e14d74d27510e315baece79db759323278326cf Mon Sep 17 00:00:00 2001 From: mattbauer Date: Thu, 26 Feb 2009 18:10:01 -0600 Subject: [PATCH] conditions on has_many in has_many through used when creating records --- .../associations/has_many_through_association.rb | 4 ++++ .../has_many_through_associations_test.rb | 6 ++++++ activerecord/test/models/post.rb | 2 ++ activerecord/test/schema/schema.rb | 1 + 4 files changed, 13 insertions(+), 0 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 2eeeb28..ed90091 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -101,6 +101,10 @@ module ActiveRecord if @reflection.options[:source_type] join_attributes.merge!(@reflection.source_reflection.options[:foreign_type] => associate.class.base_class.name.to_s) end + through_reflection_conditions = @reflection.through_reflection.options[:conditions] + if through_reflection_conditions.is_a?(Hash) + join_attributes.merge!(through_reflection_conditions) + end join_attributes end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 1e5d1a0..2a9253d 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -128,6 +128,12 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal peeps + 1, posts(:thinking).people.count end + def test_associate_with_create_with_through_having_conditions + impatient_people = posts(:thinking).impatient_people.count + posts(:thinking).impatient_people.create!(:first_name => 'foo') + assert_equal impatient_people + 1, posts(:thinking).impatient_people.count + end + def test_associate_with_create_exclamation_and_no_options peeps = posts(:thinking).people.count posts(:thinking).people.create!(:first_name => 'foo') diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 374e536..7392515 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -69,6 +69,8 @@ class Post < ActiveRecord::Base :after_add => lambda {|owner, reader| log(:added, :after, reader.first_name) }, :before_remove => lambda {|owner, reader| log(:removed, :before, reader.first_name) }, :after_remove => lambda {|owner, reader| log(:removed, :after, reader.first_name) } + has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true } + has_many :impatient_people, :through => :skimmers, :source => :person def self.top(limit) ranked_by_comments.limit(limit) diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 74a8939..4dcaee4 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -350,6 +350,7 @@ ActiveRecord::Schema.define do create_table :readers, :force => true do |t| t.integer :post_id, :null => false t.integer :person_id, :null => false + t.boolean :skimmer, :default => false end create_table :shape_expressions, :force => true do |t| -- 1.6.0.4