Class: Parse::Constraint::ObjectIdConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- Parse::Constraint::ObjectIdConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
A constraint for matching by a specific objectId value.
# where this Parse object equals the object in the column field.
q.where :field => Parse::Pointer("Field", "someObjectId")
# alias, shorthand when we infer :field maps to Field parse class.
q.where :field.pointer_id => "someObjectId"
# "field":"__type":"Pointer","className":"Field","objectId":"someObjectId"}
class Artist < Parse::Object end
class Song < Parse::Object belongs_to :artist end
artist = Artist.first # get any artist artist_id = artist.id # ex. artist.id
# find all songs for this artist object Song.all :artist => artist
In some cases, you do not have the Parse object, but you have its objectId.
You can use the objectId in the query as follows:
# shorthand if you are using convention. Will infer class Artist
Song.all :artist.pointer_id => artist_id
# other approaches, same result Song.all :artist.pointer_id => artist # safely supported Parse::Pointer Song.all :artist => Artist.pointer(artist_id) Song.all :artist => Parse::Pointer.new("Artist", artist_id)
Instance Method Summary collapse
-
#build ⇒ Hash
The compiled constraint.
-
#pointer_id ⇒ ObjectIdConstraint
A registered method on a symbol to create the constraint.
Instance Method Details
#build ⇒ Hash
Returns the compiled constraint.
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 |
# File 'lib/parse/query/constraints.rb', line 849 def build className = operand.to_parse_class value = formatted_value # if it is already a pointer value, just return the constraint. Allows for # supporting strings, symbols and pointers. return { @operation.operand => value } if value.is_a?(Parse::Pointer) begin klass = className.constantize rescue NameError klass = Parse::Model.find_class className end unless klass.present? && klass.is_a?(Parse::Object) == false raise ArgumentError, "#{self.class}: No Parse class defined for #{operand} as '#{className}'" end # allow symbols value = value.to_s if value.is_a?(Symbol) unless value.is_a?(String) && value.strip.present? raise ArgumentError, "#{self.class}: value must be of string type representing a Parse object id." end value.strip! return { @operation.operand => klass.pointer(value) } end |
#pointer_id ⇒ ObjectIdConstraint
A registered method on a symbol to create the constraint. The
operator is registered as :id and installed on Symbol as
pointer_id.
846 |
# File 'lib/parse/query/constraints.rb', line 846 register :id |