aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/belongs_to_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/belongs_to_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb10
1 files changed, 8 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index d311b0c572..1abea8d831 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -3,7 +3,7 @@ module ActiveRecord
module Associations
class BelongsToAssociation < AssociationProxy #:nodoc:
def create(attributes = {})
- replace(@reflection.create_association(attributes))
+ new_record(:create_association, attributes)
end
def create!(attributes = {})
@@ -11,7 +11,7 @@ module ActiveRecord
end
def build(attributes = {})
- replace(@reflection.build_association(attributes))
+ new_record(:build_association, attributes)
end
def replace(record)
@@ -34,6 +34,12 @@ module ActiveRecord
end
private
+ def new_record(method, attributes)
+ record = scoped.scoping { @reflection.send(method, attributes) }
+ replace(record)
+ record
+ end
+
def update_counters(record)
counter_cache_name = @reflection.counter_cache_column