aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_one_through_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_one_through_association.rb34
1 files changed, 14 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb
index c9ae930e93..dcd74e7346 100644
--- a/activerecord/lib/active_record/associations/has_one_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_through_association.rb
@@ -4,37 +4,31 @@ module ActiveRecord
class HasOneThroughAssociation < HasOneAssociation
include ThroughAssociation
- def replace(new_value)
- create_through_record(new_value)
- @target = new_value
+ def replace(record)
+ create_through_record(record)
+ self.target = record
end
private
- def create_through_record(new_value)
- proxy = @owner.send(@reflection.through_reflection.name) ||
- @owner.send(:association_instance_get, @reflection.through_reflection.name)
- record = proxy.target
+ def create_through_record(record)
+ through_proxy = @owner.send(:association_proxy, @reflection.through_reflection.name)
+ through_record = through_proxy.send(:load_target)
- if record && !new_value
- record.destroy
- elsif new_value
- attributes = construct_join_attributes(new_value)
+ if through_record && !record
+ through_record.destroy
+ elsif record
+ attributes = construct_join_attributes(record)
- if record
- record.update_attributes(attributes)
+ if through_record
+ through_record.update_attributes(attributes)
elsif @owner.new_record?
- proxy.build(attributes)
+ through_proxy.build(attributes)
else
- proxy.create(attributes)
+ through_proxy.create(attributes)
end
end
end
-
- def find_target
- update_stale_state
- scoped.first
- end
end
end
end