aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_one_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/has_one_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
new file mode 100644
index 0000000000..74e82f146a
--- /dev/null
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -0,0 +1,48 @@
+module ActiveRecord
+ module Associations
+ class HasOneAssociation < BelongsToAssociation #:nodoc:
+ def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options)
+ super
+
+ construct_sql
+ end
+
+ def replace(obj, dont_save = false)
+ load_target
+ unless @target.nil?
+ @target[@association_class_primary_key_name] = nil
+ @target.save unless @owner.new_record?
+ end
+
+ if obj.nil?
+ @target = nil
+ else
+ raise_on_type_mismatch(obj)
+
+ obj[@association_class_primary_key_name] = @owner.id unless @owner.new_record?
+ @target = obj
+ end
+
+ @loaded = true
+ unless @owner.new_record? or obj.nil? or dont_save
+ return (obj.save ? obj : false)
+ else
+ return obj
+ end
+ end
+
+ private
+ def find_target
+ @association_class.find_first(@finder_sql, @options[:order])
+ end
+
+ def target_obsolete?
+ false
+ end
+
+ def construct_sql
+ @finder_sql = "#{@association_class_primary_key_name} = #{@owner.quoted_id}#{@options[:conditions] ? " AND " + @options[:conditions] : ""}"
+ end
+ end
+ end
+end