Sha256: 1a2a9dfbb0aeab0a23b9e46436c20f403b3b267ec69c64ccad2bf6d682ef81db

Contents?: true

Size: 1.68 KB

Versions: 97

Compression:

Stored size: 1.68 KB

Contents

module ActiveRecord
  module Associations
    class BelongsToAssociation < AssociationProxy #:nodoc:
      def create(attributes = {})
        replace(@reflection.klass.create(attributes))
      end

      def build(attributes = {})
        replace(@reflection.klass.new(attributes))
      end

      def replace(record)
        counter_cache_name = @reflection.counter_cache_column

        if record.nil?
          if counter_cache_name && @owner[counter_cache_name] && !@owner.new_record?
            @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name]
          end

          @target = @owner[@reflection.primary_key_name] = nil
        else
          raise_on_type_mismatch(record)

          if counter_cache_name && !@owner.new_record?
            @reflection.klass.increment_counter(counter_cache_name, record.id)
            @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name]
          end

          @target = (AssociationProxy === record ? record.target : record)
          @owner[@reflection.primary_key_name] = record.id unless record.new_record?
          @updated = true
        end

        loaded
        record
      end
      
      def updated?
        @updated
      end
      
      private
        def find_target
          @reflection.klass.find(
            @owner[@reflection.primary_key_name], 
            :conditions => conditions,
            :include    => @reflection.options[:include]
          )
        end

        def foreign_key_present
          !@owner[@reflection.primary_key_name].nil?
        end
    end
  end
end

Version data entries

97 entries across 97 versions & 7 rubygems

Version Path
jstorimer-deep-test-2.0.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
jstorimer-deep-test-1.4.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
jstorimer-deep-test-1.3.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
jstorimer-deep-test-1.2.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
jstorimer-deep-test-1.1.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
jstorimer-deep-test-1.0.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
jstorimer-deep-test-0.2.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
jstorimer-deep-test-0.1.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
activerecord-1.14.1 lib/active_record/associations/belongs_to_association.rb
activerecord-1.14.2 lib/active_record/associations/belongs_to_association.rb
activerecord-1.14.0 lib/active_record/associations/belongs_to_association.rb
activerecord-1.14.3 lib/active_record/associations/belongs_to_association.rb
activerecord-1.14.4 lib/active_record/associations/belongs_to_association.rb
activerecord-1.15.2 lib/active_record/associations/belongs_to_association.rb
activerecord-1.15.3 lib/active_record/associations/belongs_to_association.rb
activerecord-1.15.1 lib/active_record/associations/belongs_to_association.rb
activerecord-1.15.0 lib/active_record/associations/belongs_to_association.rb
activerecord-1.15.4 lib/active_record/associations/belongs_to_association.rb
activerecord-1.15.5 lib/active_record/associations/belongs_to_association.rb
activerecord-1.15.6 lib/active_record/associations/belongs_to_association.rb