aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-03 23:48:53 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-03 16:24:32 -0800
commit40afcade0dc1450e765a91fc15a6ac6d442c9826 (patch)
tree4d205b197a761fcd8b74854ebf4cc0139277a280 /activerecord/lib/active_record/associations
parent2120da7f733ba33183a42e71256db9652c5f5fcc (diff)
downloadrails-40afcade0dc1450e765a91fc15a6ac6d442c9826.tar.gz
rails-40afcade0dc1450e765a91fc15a6ac6d442c9826.tar.bz2
rails-40afcade0dc1450e765a91fc15a6ac6d442c9826.zip
Remove undocumented feature from has_one where you could pass false as the second parameter to build_assoc or create_assoc, and the existing associated object would be untouched (the foreign key would not be nullified, and it would not be deleted). If you want behaviour similar to this you can do the following things:
* Use :dependent => :nullify (or don't specify :dependent) if you want to prevent the existing associated object from being deleted * Use has_many if you actually want multiple associated objects * Explicitly set the foreign key if, for some reason, you really need to have multiple objects associated with the same has_one. E.g. previous = obj.assoc obj.create_assoc previous.update_attributes(:obj_id => obj.id)
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb28
1 files changed, 8 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 5716bef524..f1e01197b5 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -4,22 +4,22 @@ module ActiveRecord
class HasOneAssociation < AssociationProxy #:nodoc:
include HasAssociation
- def create(attrs = {}, replace_existing = true)
- new_record(replace_existing) do |reflection|
+ def create(attrs = {})
+ new_record do |reflection|
attrs = merge_with_conditions(attrs)
reflection.create_association(attrs)
end
end
- def create!(attrs = {}, replace_existing = true)
- new_record(replace_existing) do |reflection|
+ def create!(attrs = {})
+ new_record do |reflection|
attrs = merge_with_conditions(attrs)
reflection.create_association!(attrs)
end
end
- def build(attrs = {}, replace_existing = true)
- new_record(replace_existing) do |reflection|
+ def build(attrs = {})
+ new_record do |reflection|
attrs = merge_with_conditions(attrs)
reflection.build_association(attrs)
end
@@ -82,21 +82,9 @@ module ActiveRecord
construct_owner_attributes
end
- def new_record(replace_existing)
- # Make sure we load the target first, if we plan on replacing the existing
- # instance. Otherwise, if the target has not previously been loaded
- # elsewhere, the instance we create will get orphaned.
- load_target if replace_existing
+ def new_record
record = scoped.scoping { yield @reflection }
-
- if replace_existing
- replace(record, true)
- else
- record[@reflection.foreign_key] = @owner.id if @owner.persisted?
- self.target = record
- set_inverse_instance(record)
- end
-
+ replace(record, true)
record
end