Ensure hm:t#create respects source associations hash conditions [#2090 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
mattbauer
2009-06-21 17:35:04 +01:00
committed by Pratik Naik
parent 9cb8c812f2
commit 83c1934003
4 changed files with 16 additions and 0 deletions

View File

@@ -94,10 +94,17 @@ module ActiveRecord
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)
if @reflection.options[:source_type]
join_attributes.merge!(@reflection.source_reflection.options[:foreign_type] => associate.class.base_class.name.to_s)
end
if @reflection.through_reflection.options[:conditions].is_a?(Hash)
join_attributes.merge!(@reflection.through_reflection.options[:conditions])
end
join_attributes
end

View File

@@ -157,6 +157,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')

View File

@@ -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)

View File

@@ -364,6 +364,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|