Class/Module Index [+]

Quicksearch

Mongoid::Relations::Macros::ClassMethods

Public Instance Methods

belongs_to(name, options = {}, &block) click to toggle source

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

@example Define the relation.

class Game
  include Mongoid::Document
  belongs_to :person
end

class Person
  include Mongoid::Document
  has_one :game
end

@param [ Symbol ] name The name of the relation. @param [ Hash ] options The relation options. @param [ Proc ] block Optional block for defining extensions.

# File lib/mongoid/relations/macros.rb, line 139
def belongs_to(name, options = {}, &block)
  meta = reference_one_to_one(name, options, Referenced::In, &block)
  aliased_fields[name.to_s] = meta.foreign_key
  touchable(meta)
  add_counter_cache_callbacks(meta) if meta.counter_cached?
  meta
end
embedded_in(name, options = {}, &block) click to toggle source

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.

@example Define the relation.

class Person
  include Mongoid::Document
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :person
end

@param [ Symbol ] name The name of the relation. @param [ Hash ] options The relation options. @param [ Proc ] block Optional block for defining extensions.

# File lib/mongoid/relations/macros.rb, line 52
def embedded_in(name, options = {}, &block)
  if ancestors.include?(Mongoid::Versioning)
    raise Errors::VersioningNotOnRoot.new(self)
  end
  meta = characterize(name, Embedded::In, options, &block)
  self.embedded = true
  relate(name, meta)
  builder(name, meta).creator(name, meta)
  add_counter_cache_callbacks(meta) if meta.counter_cached?
  meta
end
embeds_many(name, options = {}, &block) click to toggle source

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.

@example Define the relation.

class Person
  include Mongoid::Document
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :person
end

@param [ Symbol ] name The name of the relation. @param [ Hash ] options The relation options. @param [ Proc ] block Optional block for defining extensions.

# File lib/mongoid/relations/macros.rb, line 83
def embeds_many(name, options = {}, &block)
  meta = characterize(name, Embedded::Many, options, &block)
  self.cyclic = true if options[:cyclic]
  relate(name, meta)
  embed(name, meta)
  validates_relation(meta)
  meta
end
embeds_one(name, options = {}, &block) click to toggle source

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.

@example Define the relation.

class Person
  include Mongoid::Document
  embeds_one :name
end

class Name
  include Mongoid::Document
  embedded_in :person
end

@param [ Symbol ] name The name of the relation. @param [ Hash ] options The relation options. @param [ Proc ] block Optional block for defining extensions.

# File lib/mongoid/relations/macros.rb, line 111
def embeds_one(name, options = {}, &block)
  meta = characterize(name, Embedded::One, options, &block)
  self.cyclic = true if options[:cyclic]
  relate(name, meta)
  embed(name, meta)
  builder(name, meta).creator(name, meta)
  validates_relation(meta)
  meta
end
has_and_belongs_to_many(name, options = {}, &block) click to toggle source

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

@example 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

@param [ Symbol ] name The name of the relation. @param [ Hash ] options The relation options. @param [ Proc ] block Optional block for defining extensions.

@since 2.0.0.rc.1

# File lib/mongoid/relations/macros.rb, line 195
def has_and_belongs_to_many(name, options = {}, &block)
  meta = characterize(name, Referenced::ManyToMany, options, &block)
  relate(name, meta)
  reference(meta, Array)
  autosave(meta)
  validates_relation(meta)
  synced(meta)
  meta
end
has_many(name, options = {}, &block) click to toggle source

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

@example Define the relation.

class Person
  include Mongoid::Document
  has_many :posts
end

class Game
  include Mongoid::Document
  belongs_to :person
end

@param [ Symbol ] name The name of the relation. @param [ Hash ] options The relation options. @param [ Proc ] block Optional block for defining extensions.

# File lib/mongoid/relations/macros.rb, line 165
def has_many(name, options = {}, &block)
  meta = characterize(name, Referenced::Many, options, &block)
  relate(name, meta)
  ids_getter(name, meta).ids_setter(name, meta)
  reference(meta)
  autosave(meta)
  validates_relation(meta)
  meta
end
has_one(name, options = {}, &block) click to toggle source

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

@example Define the relation.

class Game
  include Mongoid::Document
  belongs_to :person
end

class Person
  include Mongoid::Document
  has_one :game
end

@param [ Symbol ] name The name of the relation. @param [ Hash ] options The relation options. @param [ Proc ] block Optional block for defining extensions.

# File lib/mongoid/relations/macros.rb, line 223
def has_one(name, options = {}, &block)
  reference_one_to_one(name, options, Referenced::One, &block)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.