Sha256: 076b27c7090590dcda9172470c688e52e92e1d7e80a071da5dedc803f06e1715

Contents?: true

Size: 1.77 KB

Versions: 25

Compression:

Stored size: 1.77 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.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],
            :select     => @reflection.options[:select],
            :conditions => conditions,
            :include    => @reflection.options[:include],
            :readonly   => @reflection.options[:readonly]
          )
        end

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

Version data entries

25 entries across 25 versions & 7 rubygems

Version Path
3mix-castronaut-0.5.0.2 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
masover-castronaut-0.4.4.4 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
masover-castronaut-0.4.4.5 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
masover-castronaut-0.5.0.1 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.4.1 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.4.2 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.4.3 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.4.4 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.4.5 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.4.6 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.5.0 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.5.1 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.5.2 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.5.3 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.5.4 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
radiant-0.7.2 vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
vibes-bj-1.2.2 spec/rails_root/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
vibes-bj-1.2.1 spec/rails_root/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
activerecord-2.1.1 lib/active_record/associations/belongs_to_association.rb
activerecord-2.1.2 lib/active_record/associations/belongs_to_association.rb