aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_many_through_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb37
1 files changed, 17 insertions, 20 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 292da58831..829f0ac0c5 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)
@@ -44,8 +30,18 @@ module ActiveRecord
return @target.size if loaded?
return count
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