Sha256: 812187d38ce8ffc530a8fb4d9f33eb22a0cf4ee1d4f978833cb2bd3bcef58069

Contents?: true

Size: 631 Bytes

Versions: 11

Compression:

Stored size: 631 Bytes

Contents

module Superstore
  module Associations
    class HasMany < Association
      def reader
        unless loaded?
          self.target = load_collection
        end

        target
      end

      def writer(records)
        relation = load_collection

        # TODO: Use relation.load_records with Rails 5
        relation.instance_variable_set :@records, records
        relation.instance_variable_set :@loaded, true

        self.target = relation
      end

      private

        def load_collection
          association_class.where(reflection.foreign_key => owner.try(reflection.primary_key))
        end

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
superstore-2.4.4 lib/superstore/associations/has_many.rb
superstore-2.4.3 lib/superstore/associations/has_many.rb
superstore-2.4.2 lib/superstore/associations/has_many.rb
superstore-2.4.1 lib/superstore/associations/has_many.rb
superstore-2.4.0 lib/superstore/associations/has_many.rb
superstore-2.3.0 lib/superstore/associations/has_many.rb
superstore-2.2.0 lib/superstore/associations/has_many.rb
superstore-2.1.3 lib/superstore/associations/has_many.rb
superstore-2.1.2 lib/superstore/associations/has_many.rb
superstore-2.1.1 lib/superstore/associations/has_many.rb
superstore-2.1.0 lib/superstore/associations/has_many.rb