aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-09-18 10:26:56 +0000
committerMichael Koziarski <michael@koziarski.com>2007-09-18 10:26:56 +0000
commit7010ee361ec71ed1a37962897cebc8684d90aa35 (patch)
treeb0d7d0b16037efc9c67fb87ccf0856bdc51974f2 /activerecord/lib/active_record/associations
parent4a1388adeab6aaf0ef28f975e480e22061bb1aad (diff)
downloadrails-7010ee361ec71ed1a37962897cebc8684d90aa35.tar.gz
rails-7010ee361ec71ed1a37962897cebc8684d90aa35.tar.bz2
rails-7010ee361ec71ed1a37962897cebc8684d90aa35.zip
Stop users from calling .create on a has_many / habtm association when the owner is a new_record?
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7511 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb11
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb21
2 files changed, 29 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index b0be44ff42..460f410cf0 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -85,6 +85,7 @@ module ActiveRecord
end
def create(attrs = {})
+ ensure_owner_is_not_new
record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.create(attrs) }
@target ||= [] unless loaded?
@target << record
@@ -92,6 +93,7 @@ module ActiveRecord
end
def create!(attrs = {})
+ ensure_owner_is_not_new
record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.create!(attrs) }
@target ||= [] unless loaded?
@target << record
@@ -206,7 +208,14 @@ module ActiveRecord
def callbacks_for(callback_name)
full_callback_name = "#{callback_name}_for_#{@reflection.name}"
@owner.class.read_inheritable_attribute(full_callback_name.to_sym) || []
- end
+ end
+
+ def ensure_owner_is_not_new
+ if @owner.new_record?
+ raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
+ end
+ end
+
end
end
end
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index 917a7fa5e1..663655213a 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -15,6 +15,7 @@ module ActiveRecord
def create(attributes = {})
# Can't use Base.create because the foreign key may be a protected attribute.
+ ensure_owner_is_not_new
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr) }
else
@@ -23,6 +24,18 @@ module ActiveRecord
record
end
end
+
+ def create!(attributes = {})
+ # Can't use Base.create! because the foreign key may be a protected attribute.
+ ensure_owner_is_not_new
+ if attributes.is_a?(Array)
+ attributes.collect { |attr| create(attr) }
+ else
+ record = build(attributes)
+ insert_record(record, true) unless @owner.new_record?
+ record
+ end
+ end
def find_first
load_target.first
@@ -75,9 +88,13 @@ module ActiveRecord
load_target.size
end
- def insert_record(record)
+ def insert_record(record, force=true)
if record.new_record?
- return false unless record.save
+ if force
+ record.save!
+ else
+ return false unless record.save
+ end
end
if @reflection.options[:insert_sql]