Module: Mongoid::Factory
Overview
Instantiates documents that came from the database.
Instance Method Summary collapse
-
#build(klass, attributes = {}) ⇒ Document
Builds a new
Document
from the supplied attributes. -
#from_db(klass, attributes = {}) ⇒ Document
Builds a new
Document
from the supplied attributes loaded from the database.
Instance Method Details
#build(klass, attributes = {}) ⇒ Document
Builds a new Document
from the supplied attributes.
17 18 19 20 |
# File 'lib/mongoid/factory.rb', line 17 def build(klass, attributes = {}) type = (attributes || {})["_type"] type.blank? ? klass.new(attributes) : type.constantize.new(attributes) end |
#from_db(klass, attributes = {}) ⇒ Document
Builds a new Document
from the supplied attributes loaded from the database.
32 33 34 35 |
# File 'lib/mongoid/factory.rb', line 32 def from_db(klass, attributes = {}) type = attributes["_type"] type.blank? ? klass.instantiate(attributes) : type.constantize.instantiate(attributes) end |