Class: ActiveRecord::PredicateBuilder::AssociationQueryValue

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb

Instance Method Summary collapse

Instance Method Details

#idsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb', line 19

def ids
  case value
  when Relation
    value.select_values.empty? ? value.select(primary_key) : value
  when Array
    value.map { |v| convert_to_id(v) }
  else
    # CPK
    # convert_to_id(value)
    if value.nil?
      nil
    elsif value.respond_to?(:composite?) && value.composite?
      value.class.primary_keys.zip(value.id)
    else
      convert_to_id(value)
    end
  end
end

#queriesObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb', line 4

def queries
  # CPK
  if associated_table.join_foreign_key.is_a?(Array)
    if ids.is_a?(ActiveRecord::Relation)
      ids.map do |id|
        associated_table.join_foreign_key.zip(id.id).to_h
      end
    else
      [associated_table.join_foreign_key.zip(ids).to_h]
    end
  else
    [associated_table.join_foreign_key => ids]
  end
end