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

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

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



52
53
54
55
56
57
# File 'lib/mongoid/relations/macros.rb', line 52

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.



78
79
80
81
82
83
# File 'lib/mongoid/relations/macros.rb', line 78

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.



104
105
106
107
108
109
110
# File 'lib/mongoid/relations/macros.rb', line 104

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

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

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
  referenced_in :person
end

class Person
  include Mongoid::Document
  references_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.



130
131
132
133
134
135
136
# File 'lib/mongoid/relations/macros.rb', line 130

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

#references_and_referenced_in_many(name, options = {}, &block) ⇒ Object Also known as: has_and_belongs_to_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
  references_and_referenced_in_many :preferences
end

class Preference
  include Mongoid::Document
  references_and_referenced_in_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



190
191
192
193
194
195
196
# File 'lib/mongoid/relations/macros.rb', line 190

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

#references_many(name, options = {}, &block) ⇒ Object Also known as: has_many_related, has_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
  references_many :posts
end

class Game
  include Mongoid::Document
  referenced_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.



158
159
160
161
162
163
164
165
166
# File 'lib/mongoid/relations/macros.rb', line 158

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

#references_one(name, options = {}, &block) ⇒ Object Also known as: has_one_related, has_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
  referenced_in :person
end

class Person
  include Mongoid::Document
  references_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.



217
218
219
220
221
222
223
224
# File 'lib/mongoid/relations/macros.rb', line 217

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