Class: Parse::Constraint::MatchesKeyInQueryConstraint

Inherits:
Parse::Constraint show all
Defined in:
lib/parse/query/constraints.rb

Overview

Equivalent to the ‘$select` Parse query operation but for key matching. This matches objects where a field’s value equals another field’s value from a different query. Useful for performing join-like operations where fields from different classes match.

# Find users where user.company equals customer.company
customer_query = Customer.where(:active => true)
user_query = User.where(:company.matches_key => { key: "company", query: customer_query })

# If the local field has the same name as the remote field, you can omit the key
# assumes key: 'company'
user_query = User.where(:company.matches_key => customer_query)

Instance Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    the compiled constraint.



2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
# File 'lib/parse/query/constraints.rb', line 2213

def build
  remote_field_name = @operation.operand
  query = nil

  if @value.is_a?(Hash)
    res = @value.symbolize_keys
    remote_field_name = res[:key] || remote_field_name
    query = res[:query]
    unless query.is_a?(Parse::Query)
      raise ArgumentError, "Invalid Parse::Query object provided in :query field of value: #{@operation.operand}.matches_key_in_query => #{@value}"
    end
    query = query.compile(encode: false, includeClassName: true)
  elsif @value.is_a?(Parse::Query)
    # if its a query, then assume key is the same name as operand.
    query = @value.compile(encode: false, includeClassName: true)
  else
    raise ArgumentError, "Invalid `:matches_key_in_query` query constraint. It should follow the format: :field.matches_key_in_query => { key: 'key', query: '<Parse::Query>' }"
  end

  { @operation.operand => { :$select => { key: remote_field_name, query: query } } }
end

#matches_keyMatchesKeyInQueryConstraint



2208
# File 'lib/parse/query/constraints.rb', line 2208

constraint_keyword :$select

#matches_key_in_queryMatchesKeyInQueryConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :field.matches_key_in_query => { key: "remote_field", query: query }
q.where :field.matches_key_in_query => query # assumes same field name

Returns:



# File 'lib/parse/query/constraints.rb', line 2198