Module: Parse::Associations::BelongsTo

Included in:
Object
Defined in:
lib/parse/model/associations/belongs_to.rb

Overview

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.

Examples:

class Author < Parse::Object
	property :name
end

class Post < Parse::Object
	belongs_to :author
end

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

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

# Follow the author pointer and get name
post.author.name

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

See Also:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.referencesHash

A hash mapping of all belongs_to associations for this model.

Returns:

# 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.

Examples:

# 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

Parameters:

  • 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.

Returns:

  • (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

#key?Boolean

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.

Examples:


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

post = Post.new
post.author? # => false
post.author = Author.new
post.author? # => true

Returns:

  • (Boolean)

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

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