I have a MongoMapper model called LogInfo that has a number of fields already defined (:user_id, :user_key, :message, etc)
However, I’d also like the user to embed their own JSON objects as an EmbeddedDocument.
Is there a way to use anonymous objects to create a custom object in MongoMapper? Would I have to resort to the native Ruby driver instead? The final object would look something like this:
{
user_id: 393,
user_key: "kdIekHG32Je",
message: "Application error",
custom_data: {
browser: "Firefox 8.0",
location: {
lat: 34.323,
lon: -14.091
}
}
}
where the custom_data field is something random the app user can provide.
Sure. You don’t have to resort to the Ruby driver.
MongoMapper turns objects into a MongoDB representation by calling:
The
to_mongomethod is already defined for a bunch of standard classes, and you can define it for your own classes too. See lib/mongo_mapper/extensions for all the classes that haveto_mongodefined and http://mongomapper.com/documentation/types.html for the documentation on custom types.BUT, if you’re content to let your users just hand you a hash, it’ll just work.