Module: Switchman::ActiveRecord::Associations::CollectionAssociation

Defined in:
lib/switchman/active_record/associations.rb

Instance Method Summary collapse

Instance Method Details

#_create_recordObject



47
48
49
# File 'lib/switchman/active_record/associations.rb', line 47

def _create_record(*)
  shard.activate { super }
end

#find_targetObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/switchman/active_record/associations.rb', line 26

def find_target
  shards = if reflection.options[:multishard] && owner.respond_to?(:associated_shards)
             owner.associated_shards
           else
             [shard]
           end
  # activate both the owner and the target's shard category, so that Reflection#join_id_for,
  # when called for the owner, will be returned relative to shard the query will execute on
  Shard.with_each_shard(shards,
                        [klass.connection_class_for_self, owner.class.connection_class_for_self].uniq) do
    if reflection.options[:multishard] && owner.respond_to?(:associated_shards) && reflection.has_scope?
      # Prevent duplicate results when reflection has a scope (when it would use the skip_statement_cache? path)
      # otherwise, the super call will set the shard_value to the object, causing it to iterate too many times
      # over the associated shards
      scope.shard(Shard.current(scope.klass.connection_class_for_self), :association).to_a
    else
      super
    end
  end
end