Sha256: 1891a3f45bcb55fe046cd257226d30d5102aa813ccbede0828faa9662bdd9e70

Contents?: true

Size: 1.78 KB

Versions: 17

Compression:

Stored size: 1.78 KB

Contents

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

      def build(attributes = {})
        replace(@reflection.build_association(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

17 entries across 16 versions & 9 rubygems

Version Path
p8-castronaut-0.6.1.1 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.6.0 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.6.1 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.7.4 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
relevance-castronaut-0.7.5 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
nbudin-castronaut-0.7.5 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/activerecord/pkg/activerecord-2.2.2/lib/active_record/associations/belongs_to_association.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
factorylabs-castronaut-0.7.5 vendor/activerecord/lib/active_record/associations/belongs_to_association.rb
activerecord-2.2.3 lib/active_record/associations/belongs_to_association.rb
webroar-0.2.2 src/admin_panel/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
activerecord-2.3.2 lib/active_record/associations/belongs_to_association.rb
activerecord-2.2.2 lib/active_record/associations/belongs_to_association.rb
mack-active_record-0.8.2 lib/gems/activerecord-2.2.2/lib/active_record/associations/belongs_to_association.rb
mack-active_record-0.8.3 lib/gems/activerecord-2.2.2/lib/active_record/associations/belongs_to_association.rb
mack-active_record-0.8.3.1 lib/gems/activerecord-2.2.2/lib/active_record/associations/belongs_to_association.rb
radiant-0.8.0 vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb