aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-08-10 21:02:06 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-08-10 21:02:06 +0100
commitd9c4087a9e73ea31dbb83baa26c02904039fb480 (patch)
tree4fc5c22fd1a1101ca2d892f92fcbd14036eca121
parentf97dae5ebe2f19273d3f92e5ea9baba788c8e89f (diff)
downloadrails-d9c4087a9e73ea31dbb83baa26c02904039fb480.tar.gz
rails-d9c4087a9e73ea31dbb83baa26c02904039fb480.tar.bz2
rails-d9c4087a9e73ea31dbb83baa26c02904039fb480.zip
Unify hm:t#create and create! implementation
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb30
1 files changed, 13 insertions, 17 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..c662bb8ec4 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 = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association
+ 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