Module: Mongoid::Associations::ClassMethods
- Defined in:
- lib/mongoid/associations.rb
Instance Method Summary collapse
-
#belongs_to_related(name, options = {}, &block) ⇒ Object
Adds a relational association from the child Document to a Document in another database or collection.
-
#embedded? ⇒ Boolean
Gets whether or not the document is embedded.
-
#embedded_in(name, options = {}, &block) ⇒ Object
(also: #belongs_to)
Adds the association back to the parent document.
-
#embeds_many(name, options = {}, &block) ⇒ Object
(also: #embed_many, #has_many)
Adds the association from a parent document to its children.
-
#embeds_one(name, options = {}, &block) ⇒ Object
(also: #embed_one, #has_one)
Adds the association from a parent document to its child.
-
#has_many_related(name, options = {}, &block) ⇒ Object
Adds a relational association from the Document to many Documents in another database or collection.
-
#has_one_related(name, options = {}, &block) ⇒ Object
Adds a relational association from the Document to one Document in another database or collection.
-
#reflect_on_association(name) ⇒ Object
Returns the macro associated with the supplied association name.
Instance Method Details
#belongs_to_related(name, options = {}, &block) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/mongoid/associations.rb', line 69 def (name, = {}, &block) opts = optionize(name, , fk(name, ), &block) associate(Associations::BelongsToRelated, opts) field(opts.foreign_key, :type => Mongoid.use_object_ids ? BSON::ObjectId : String) index(opts.foreign_key) unless end |
#embedded? ⇒ Boolean
Gets whether or not the document is embedded.
Example:
Person.embedded?
Returns:
true
if embedded, false
if not.
85 86 87 |
# File 'lib/mongoid/associations.rb', line 85 def !!self. end |
#embedded_in(name, options = {}, &block) ⇒ Object Also known as: belongs_to
Adds the association 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 association calling persistence methods on the child object will cause a save to fail.
Options:
name: A Symbol
that matches the name of the parent class.
Example:
class Person
include Mongoid::Document
:addresses
end
class Address
include Mongoid::Document
:person, :inverse_of => :addresses
end
109 110 111 112 113 114 115 |
# File 'lib/mongoid/associations.rb', line 109 def (name, = {}, &block) unless .has_key?(:inverse_of) raise Errors::InvalidOptions.new("Options for embedded_in association must include :inverse_of") end self. = true associate(Associations::EmbeddedIn, optionize(name, , nil, &block)) end |
#embeds_many(name, options = {}, &block) ⇒ Object Also known as: embed_many, has_many
Adds the association from a parent document to its children. The name of the association needs to be a pluralized form of the child class name.
Options:
name: A Symbol
that is the plural child class name.
Example:
class Person
include Mongoid::Document
:addresses
end
class Address
include Mongoid::Document
:person, :inverse_of => :addresses
end
138 139 140 141 142 143 144 145 |
# File 'lib/mongoid/associations.rb', line 138 def (name, = {}, &block) associate(Associations::EmbedsMany, optionize(name, , nil, &block)) unless name == :versions after_update do |document| document.(name) end end end |
#embeds_one(name, options = {}, &block) ⇒ Object Also known as: embed_one, has_one
Adds the association from a parent document to its child. The name of the association needs to be a singular form of the child class name.
Options:
name: A Symbol
that is the plural child class name.
Example:
class Person
include Mongoid::Document
:name
end
class Name
include Mongoid::Document
:person
end
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/mongoid/associations.rb', line 169 def (name, = {}, &block) opts = optionize(name, , nil, &block) type = Associations::EmbedsOne associate(type, opts) add_builder(type, opts) add_creator(type, opts) after_update do |document| document.(name) end end |
#has_many_related(name, options = {}, &block) ⇒ Object
197 198 199 200 201 202 |
# File 'lib/mongoid/associations.rb', line 197 def (name, = {}, &block) associate(Associations::HasManyRelated, optionize(name, , fk(self.name, ), &block)) before_save do |document| document.update_associations(name) end end |
#has_one_related(name, options = {}, &block) ⇒ Object
217 218 219 220 221 222 |
# File 'lib/mongoid/associations.rb', line 217 def (name, = {}, &block) associate(Associations::HasOneRelated, optionize(name, , fk(self.name, ), &block)) before_save do |document| document.update_association(name) end end |
#reflect_on_association(name) ⇒ Object
Returns the macro associated with the supplied association name. This will return has_one, has_many, belongs_to or nil.
Options:
name: The association name.
Example:
Person.reflect_on_association(:addresses)
234 235 236 237 |
# File 'lib/mongoid/associations.rb', line 234 def reflect_on_association(name) association = associations[name.to_s] association ? association.macro : nil end |