Sha256: e69ef8586cfba1f9be5405161452d918c08e7151385b83c9dd5ea93c3221a301

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

module ActiveRecord
  module Associations
    class BelongsToAssociation < AssociationProxy #:nodoc:

      def reset
        @target = nil
        @loaded = false
      end

      def create(attributes = {})
        record = build(attributes)
        record.save
        record
      end

      def build(attributes = {})
        record = @association_class.new(attributes)
        replace(record, true)
        record
      end

      def replace(obj, dont_save = false)
        if obj.nil?
          @target = @owner[@association_class_primary_key_name] = nil
        else
          raise_on_type_mismatch(obj) unless obj.nil?

          @target = obj
          @owner[@association_class_primary_key_name] = obj.id unless obj.new_record?
        end
        @loaded = true

        return (@target.nil? ? nil : self)
      end

      private
        def find_target
          if @options[:conditions]
            @association_class.find_on_conditions(@owner[@association_class_primary_key_name], interpolate_sql(@options[:conditions]))
          else
            @association_class.find(@owner[@association_class_primary_key_name])
          end
        end

        def foreign_key_present
          !@owner[@association_class_primary_key_name].nil?
        end

        def target_obsolete?
          @owner[@association_class_primary_key_name] != @target.id
        end

        def construct_sql
          # no sql to construct
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activerecord-1.5.1 lib/active_record/associations/belongs_to_association.rb
activerecord-1.8.0 lib/active_record/associations/belongs_to_association.rb
activerecord-1.9.0 lib/active_record/associations/belongs_to_association.rb
activerecord-1.7.0 lib/active_record/associations/belongs_to_association.rb
activerecord-1.6.0 lib/active_record/associations/belongs_to_association.rb
activerecord-1.9.1 lib/active_record/associations/belongs_to_association.rb