Drew Cimino
unread,Jan 27, 2012, 4:42:50 PM1/27/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to MongoMapper
So the main Document I'm using has 3 EmbeddedDocuments all nested
inside each other (one-to-many). Since I don't know exactly how many
of the embedded documents will get constructed, I decided the easiest
way to do build the Document would be to factor out the building into
the constructor for each model, rather than serializing all the data
on the fly and using a giant pile of nested build() spaghetti code to
build a model.
When I try to build a new nested model, like so:
class Resource
include MongoMapper::Document
MongoMapper.database = "db"
key :name, String
many :things
def initialize(data)
@name = data[:name]
self.populate_things(data[:things])
end
def populate_things(new_things)
new_things.each do |new_thing|
@things.push(Thing.new(new_thing))
end
end
end
I get an error, telling me that there's no method 'push' for nil, and
I have found absolutely nothing online telling me how to access the
array of many 'things'.
What lets me do this, or if there's a better way to go about what I'm
trying to do, please enlighten me. Thanks very much.