Module: Parse::Associations::BelongsTo

Included in:
Defined in:


This association creates a one-to-one association with another Parse model. BelongsTo relation is the simplies association in which the local Parse table constains a column that has a Parse::Pointer to a foreign table record.

This association says that this class contains a foreign pointer column which references a different class. Utilizing the `belongs_to` method in defining a property in a Parse::Object subclass sets up an association between the local table and a foreign table. Specifying the `belongs_to` in the class, tells the framework that the Parse table contains a local column in its schema that has a reference to a record in a foreign table. The argument to `belongs_to` should be the singularized version of the foreign Parse::Object class. you should specify the foreign table as the snake_case singularized version of the foreign table class.

Note that the reverse relationship on the foreign class is not generated automatically. You can use a `has_one` on the foreign model to create it.


class Author < Parse::Object
	property :name

class Post < Parse::Object
	belongs_to :author

Post.references # => {:author=>"Author"}

post = Post.first # => true if has a pointer

# Follow the author pointer and get name

other_author = Author.first
# change author by setting new pointer = other_author

See Also:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details


A hash mapping of all belongs_to associations for this model.


# File 'lib/parse/model/associations/belongs_to.rb', line 55

Class Method Details

.belongs_to(key, opts = {}) ⇒ Parse::Object

Creates a one-to-one association with another Parse model.


# Assumes 'Artist' is foreign class.
belongs_to :artist

# uses Parse::User as foreign class
belongs_to :manager, as: :user

# sets attribute name to `featured_song` for foreign class Song with the remote
# column name in Parse as 'theFeaturedSong'.
belongs_to :featured_song, as: :song, field: :theFeaturedSong


  • key (Symbol)

    The singularized version of the foreign class and the name of the local column in the remote Parse table where the pointer is stored.

Options Hash (opts):

  • :field (Symbol)

    override the name of the remote column where the pointer is stored. By default this is inferred as the columnized of the key parameter.

  • :as (Symbol)

    override the inferred Parse::Object subclass. By default this is inferred as the singularized camel case version of the key parameter. This option allows you to override the typecast of foreign Parse model of the association, while allowing you to have a different accessor name.

  • :required (Boolean)

    Setting to `true`, automatically creates an ActiveModel validation of `validates_presence_of` for the association. This will not prevent the save, but affects the validation check when `valid?` is called on an instance. Default is false.


  • (Parse::Object)

    a Parse::Object subclass when using the accessor when fetching the association.

See Also:

# File 'lib/parse/model/associations/belongs_to.rb', line 59

Instance Method Details


A dynamically generated method based on the value of `key` passed to the belongs_to method, which returns true if this instance has a pointer for this field.


class Post < Parse::Object
	belongs_to :author # generates 'author?'

post = # => false = # => true


  • (Boolean)

    true if field contains a Parse::Pointer or subclass.

# File 'lib/parse/model/associations/belongs_to.rb', line 91