diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-08-10 18:19:24 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-08-10 18:19:24 -0300 |
commit | 10af9fae4a66f5f80c89ba75009cc7c7c9935a44 (patch) | |
tree | 82d77efb6addd6f4f672e4d9659c3141904e923e /activerecord/lib/active_record/associations | |
parent | 0e2fbd80e2420329738b891240d44a056cea1de4 (diff) | |
parent | d0f891ae0215d7963b3918f3847ee4c015a6b90c (diff) | |
download | rails-10af9fae4a66f5f80c89ba75009cc7c7c9935a44.tar.gz rails-10af9fae4a66f5f80c89ba75009cc7c7c9935a44.tar.bz2 rails-10af9fae4a66f5f80c89ba75009cc7c7c9935a44.zip |
Merge commit 'rails/master'
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 35 |
1 files changed, 16 insertions, 19 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 6004751dc9..214ce5959a 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -8,25 +8,11 @@ module ActiveRecord alias_method :new, :build def create!(attrs = nil) - ensure_owner_is_not_new - - transaction do - self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association! } : @reflection.create_association!) - object - end + create_record(attrs, true) end def create(attrs = nil) - ensure_owner_is_not_new - - transaction do - object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association - raise_on_type_mismatch(object) - add_record_to_target_with_callbacks(object) do |r| - insert_record(object, false) - end - object - end + create_record(attrs, false) end def destroy(*records) @@ -46,6 +32,16 @@ module ActiveRecord end protected + def create_record(attrs, force = true) + ensure_owner_is_not_new + + transaction do + object = @reflection.klass.new(attrs) + add_record_to_target_with_callbacks(object) {|r| insert_record(object, force) } + object + end + end + def target_reflection_has_associated_record? if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.primary_key_name].blank? false @@ -69,9 +65,10 @@ module ActiveRecord return false unless record.save(validate) end end - through_reflection = @reflection.through_reflection - klass = through_reflection.klass - @owner.send(@reflection.through_reflection.name).proxy_target << klass.send(:with_scope, :create => construct_join_attributes(record)) { through_reflection.create_association! } + + through_association = @owner.send(@reflection.through_reflection.name) + through_record = through_association.create!(construct_join_attributes(record)) + through_association.proxy_target << through_record end # TODO - add dependent option support |