Module: Mongoid::Associations::ClassMethods
- Defined in:
- lib/mongoid/associations.rb
Instance Method Summary collapse
- #associations ⇒ Object
-
#belongs_to(name, options = {}) ⇒ Object
Adds the association back to the parent document.
-
#belongs_to_related(name, options = {}) ⇒ Object
Adds a relational association from the child Document to a Document in another database or collection.
-
#has_many(name, options = {}) ⇒ Object
Adds the association from a parent document to its children.
-
#has_many_related(name, options = {}) ⇒ Object
Adds a relational association from the Document to many Documents in another database or collection.
-
#has_one(name, options = {}) ⇒ Object
Adds the association from a parent document to its child.
-
#has_one_related(name, options = {}) ⇒ 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
#associations ⇒ Object
38 39 40 |
# File 'lib/mongoid/associations.rb', line 38 def associations @associations ||= {}.with_indifferent_access end |
#belongs_to(name, options = {}) ⇒ Object
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 < Mongoid::Document
has_many :addresses
end
class Address < Mongoid::Document
belongs_to :person, :inverse_of => :addresses
end
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mongoid/associations.rb', line 59 def belongs_to(name, = {}) unless .has_key?(:inverse_of) raise Errors::InvalidOptions.new("Options for belongs_to association must include :inverse_of") end @embedded = true add_association( Associations::BelongsTo, Associations::Options.new(.merge(:name => name)) ) end |
#belongs_to_related(name, options = {}) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/mongoid/associations.rb', line 83 def (name, = {}) field "#{name.to_s}_id" add_association( Associations::BelongsToRelated, Associations::Options.new(.merge(:name => name)) ) end |
#has_many(name, options = {}) ⇒ Object
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 < Mongoid::Document
has_many :addresses
end
class Address < Mongoid::Document
belongs_to :person, :inverse_of => :addresses
end
108 109 110 111 112 113 |
# File 'lib/mongoid/associations.rb', line 108 def has_many(name, = {}) add_association( Associations::HasMany, Associations::Options.new(.merge(:name => name)) ) end |
#has_many_related(name, options = {}) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/mongoid/associations.rb', line 128 def (name, = {}) add_association( Associations::HasManyRelated, Associations::Options.new(.merge(:name => name, :parent_key => self.name.foreign_key)) ) before_save do |document| document.update_associations(name) end end |
#has_one(name, options = {}) ⇒ Object
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 < Mongoid::Document
has_many :addresses
end
class Address < Mongoid::Document
belongs_to :person
end
155 156 157 158 159 160 |
# File 'lib/mongoid/associations.rb', line 155 def has_one(name, = {}) add_association( Associations::HasOne, Associations::Options.new(.merge(:name => name)) ) end |
#has_one_related(name, options = {}) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/mongoid/associations.rb', line 174 def (name, = {}) add_association( Associations::HasOneRelated, Associations::Options.new(.merge(:name => name, :parent_key => self.name.foreign_key)) ) 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)
194 195 196 197 |
# File 'lib/mongoid/associations.rb', line 194 def reflect_on_association(name) association = associations[name] association ? association.macro : nil end |