From aa85a7a7e70ece22728aa3463fc3427c624d8296 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 21 Sep 2010 21:01:48 -0700 Subject: drying up construct_association --- activerecord/lib/active_record/associations.rb | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 4fcc754a23..e8a5364cd7 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1978,27 +1978,27 @@ module ActiveRecord def construct_association(record, join, row) return if record.id.to_s != join.parent.record_id(row).to_s - case join.reflection.macro - when :has_many, :has_and_belongs_to_many - collection = record.send(join.reflection.name) - collection.loaded - - return if row[join.aliased_primary_key].nil? - association = join.instantiate(row) - collection.target.push(association) - collection.__send__(:set_inverse_instance, association, record) - when :has_one + macro = join.reflection.macro + if macro == :has_one return if record.instance_variable_defined?("@#{join.reflection.name}") association = join.instantiate(row) unless row[join.aliased_primary_key].nil? set_target_and_inverse(join, association, record) - when :belongs_to + else return if row[join.aliased_primary_key].nil? association = join.instantiate(row) - set_target_and_inverse(join, association, record) - else - raise ConfigurationError, "unknown macro: #{join.reflection.macro}" + case macro + when :has_many, :has_and_belongs_to_many + collection = record.send(join.reflection.name) + collection.loaded + collection.target.push(association) + collection.__send__(:set_inverse_instance, association, record) + when :belongs_to + set_target_and_inverse(join, association, record) + else + raise ConfigurationError, "unknown macro: #{join.reflection.macro}" + end end - return association + association end def set_target_and_inverse(join, association, record) -- cgit v1.2.3