mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
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:
@@ -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
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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|
|
||||
|
||||
Reference in New Issue
Block a user