Module: Mongoid::Relations::Macros::ClassMethods

Defined in:
lib/mongoid/relations/macros.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, options = {}, &block) ⇒ Object Also known as: belongs_to_related, referenced_in

Adds a relational association from the child Document to a Document in another database or collection.

Examples:

Define the relation.


class Game
  include Mongoid::Document
  belongs_to :person
end

class Person
  include Mongoid::Document
  has_one :game
end

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash) (defaults to: {})

    The relation options.

  • block (Proc)

    Optional block for defining extensions.



139
140
141
142
143
144
145
146
# File 'lib/mongoid/relations/macros.rb', line 139

def belongs_to(name, options = {}, &block)
  characterize(name, Referenced::In, options, &block).tap do |meta|
    relate(name, meta)
    reference(meta)
    autosave(meta)
    validates_relation(meta)
  end
end

#embedded_in(name, options = {}, &block) ⇒ Object

Adds the relation back to the parent document. This macro is necessary to set the references from the child back to the parent document. If a child does not define this relation calling persistence methods on the child object will cause a save to fail.

Examples:

Define the relation.


class Person
  include Mongoid::Document
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :person
end

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash) (defaults to: {})

    The relation options.

  • block (Proc)

    Optional block for defining extensions.



61
62
63
64
65
66
# File 'lib/mongoid/relations/macros.rb', line 61

def embedded_in(name, options = {}, &block)
  characterize(name, Embedded::In, options, &block).tap do |meta|
    self.embedded = true
    relate(name, meta)
  end
end

#embeds_many(name, options = {}, &block) ⇒ Object

Adds the relation from a parent document to its children. The name of the relation needs to be a pluralized form of the child class name.

Examples:

Define the relation.


class Person
  include Mongoid::Document
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :person
end

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash) (defaults to: {})

    The relation options.

  • block (Proc)

    Optional block for defining extensions.



87
88
89
90
91
92
# File 'lib/mongoid/relations/macros.rb', line 87

def embeds_many(name, options = {}, &block)
  characterize(name, Embedded::Many, options, &block).tap do |meta|
    relate(name, meta)
    validates_relation(meta)
  end
end

#embeds_one(name, options = {}, &block) ⇒ Object

Adds the relation from a parent document to its child. The name of the relation needs to be a singular form of the child class name.

Examples:

Define the relation.


class Person
  include Mongoid::Document
  embeds_one :name
end

class Name
  include Mongoid::Document
  embedded_in :person
end

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash) (defaults to: {})

    The relation options.

  • block (Proc)

    Optional block for defining extensions.



113
114
115
116
117
118
119
# File 'lib/mongoid/relations/macros.rb', line 113

def embeds_one(name, options = {}, &block)
  characterize(name, Embedded::One, options, &block).tap do |meta|
    relate(name, meta)
    builder(name, meta).creator(name)
    validates_relation(meta)
  end
end

#has_and_belongs_to_many(name, options = {}, &block) ⇒ Object Also known as: references_and_referenced_in_many

Adds a relational many-to-many association between many of this Document and many of another Document.

Examples:

Define the relation.


class Person
  include Mongoid::Document
  has_and_belongs_to_many :preferences
end

class Preference
  include Mongoid::Document
  has_and_belongs_to_many :people
end

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash) (defaults to: {})

    The relation options.

  • block (Proc)

    Optional block for defining extensions.

Since:

  • 2.0.0.rc.1



199
200
201
202
203
204
205
206
207
# File 'lib/mongoid/relations/macros.rb', line 199

def has_and_belongs_to_many(name, options = {}, &block)
  characterize(name, Referenced::ManyToMany, options, &block).tap do |meta|
    relate(name, meta)
    reference(meta, Array)
    autosave(meta)
    validates_relation(meta)
    synced(meta)
  end
end

#has_many(name, options = {}, &block) ⇒ Object Also known as: has_many_related, references_many

Adds a relational association from a parent Document to many Documents in another database or collection.

Examples:

Define the relation.


class Person
  include Mongoid::Document
  has_many :posts
end

class Game
  include Mongoid::Document
  belongs_to :person
end

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash) (defaults to: {})

    The relation options.

  • block (Proc)

    Optional block for defining extensions.



168
169
170
171
172
173
174
175
# File 'lib/mongoid/relations/macros.rb', line 168

def has_many(name, options = {}, &block)
  characterize(name, Referenced::Many, options, &block).tap do |meta|
    relate(name, meta)
    reference(meta)
    autosave(meta)
    validates_relation(meta)
  end
end

#has_one(name, options = {}, &block) ⇒ Object Also known as: has_one_related, references_one

Adds a relational association from the child Document to a Document in another database or collection.

Examples:

Define the relation.


class Game
  include Mongoid::Document
  belongs_to :person
end

class Person
  include Mongoid::Document
  has_one :game
end

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash) (defaults to: {})

    The relation options.

  • block (Proc)

    Optional block for defining extensions.



228
229
230
231
232
233
234
235
# File 'lib/mongoid/relations/macros.rb', line 228

def has_one(name, options = {}, &block)
  characterize(name, Referenced::One, options, &block).tap do |meta|
    relate(name, meta)
    reference(meta)
    builder(name, meta).creator(name).autosave(meta)
    validates_relation(meta)
  end
end