Module: Parse::Associations::HasOne
- Included in:
- Object
- Defined in:
- lib/parse/model/associations/has_one.rb
Overview
The `has_one` creates a one-to-one association with another Parse class. This association says that the other class in the association contains a foreign pointer column which references instances of this class. If your model contains a column that is a Parse pointer to another class, you should use `belongs_to` for that association instead.
Defining a `has_one` property generates a helper query method to fetch a particular record from a foreign class. This is useful for setting up the inverse relationship accessors of a `belongs_to`. In the case of the `has_one` relationship, the `:field` option represents the name of the column of the foreign class where the Parse pointer is stored. By default, the lower-first camel case version of the Parse class name is used.
In the example below, a `Band` has a local column named `manager` which has a pointer to a `Parse::User` (:user) record. This setups up the accessor for `Band` objects to access the band's manager.
Since we know there is a column named `manager` in the `Band` class that points to a single `Parse::User`, you can setup the inverse association read accessor in the `Parse::User` class. Note, that to change the association, you need to modify the `manager` property on the band instance since it contains the `belongs_to` property.
# every band has a manager
class Band < Parse::Object
belongs_to :manager, as: :user
end
band = Band.first id: '12345'
# the user represented by this manager
user = band.manger
# every user manages a band
class Parse::User
# inverse relationship to `Band.belongs_to :manager`
has_one :band, field: :manager
end
user = Parse::User.first
user.band # similar to performing: Band.first(:manager => user)
You may optionally use `has_one` with scopes, in order to fine tune the query result. Using the example above, you can customize the query with a scope that only fetches the association if the band is approved. If the association cannot be fetched, `nil` is returned.
# adding to previous example
class Band < Parse::Object
property :approved, :boolean
property :approved_date, :date
end
# every user manages a band
class Parse::User
has_one :recently_approved, ->{ where(order: :approved_date.desc) }, field: :manager, as: :band
has_one :band_by_status, ->(status) { where(approved: status) }, field: :manager, as: :band
end
# gets the band most recently approved
user.recently_approved
# equivalent: Band.first(manager: user, order: :approved_date.desc)
# fetch the managed band that is not approved
user.band_by_status(false)
# equivalent: Band.first(manager: user, approved: false)
Class Method Summary collapse
- .has_one(key, scope = nil, opts = {}) ⇒ Parse::Object
Creates a one-to-one association with another Parse model.
Class Method Details
.has_one(key, scope = nil, opts = {}) ⇒ Parse::Object
Creates a one-to-one association with another Parse model.
# File 'lib/parse/model/associations/has_one.rb', line 83
|