Module: Mongoid::Association::Macros::ClassMethods

Defined in:
build/mongoid-7.0/lib/mongoid/association/macros.rb

Instance Method Summary collapse

Instance Method Details

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

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.



121
122
123
# File 'build/mongoid-7.0/lib/mongoid/association/macros.rb', line 121

def belongs_to(name, options = {}, &block)
  define_association!(__method__, name, options, &block)
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.



53
54
55
# File 'build/mongoid-7.0/lib/mongoid/association/macros.rb', line 53

def embedded_in(name, options = {}, &block)
  define_association!(__method__, name, options, &block)
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.



76
77
78
# File 'build/mongoid-7.0/lib/mongoid/association/macros.rb', line 76

def embeds_many(name, options = {}, &block)
  define_association!(__method__, name, options, &block)
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.



99
100
101
# File 'build/mongoid-7.0/lib/mongoid/association/macros.rb', line 99

def embeds_one(name, options = {}, &block)
  define_association!(__method__, name, options, &block)
end

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

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



167
168
169
# File 'build/mongoid-7.0/lib/mongoid/association/macros.rb', line 167

def has_and_belongs_to_many(name, options = {}, &block)
  define_association!(__method__, name, options, &block)
end

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

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.



143
144
145
# File 'build/mongoid-7.0/lib/mongoid/association/macros.rb', line 143

def has_many(name, options = {}, &block)
  define_association!(__method__, name, options, &block)
end

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

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.



189
190
191
# File 'build/mongoid-7.0/lib/mongoid/association/macros.rb', line 189

def has_one(name, options = {}, &block)
  define_association!(__method__, name, options, &block)
end