Accessing a mongodb document as a hash?

16 views
Skip to first unread message

scottmotte

unread,
Jul 19, 2009, 3:19:34 PM7/19/09
to MongoMapper
Is there a way to allow users to create custom fields using
mongomapper?

I would like to be able to do: object['custom_field'] = 'test' on the
fly. That way the user could define a custom field in a input box and
the value in the input box next to it.

couchrest does this, but I'm pretty sure it's because it returns
things as a hash and not a ruby object.

see the comments here: http://merbist.com/2009/05/17/couchdb-with-couchrest-in-5-minutes/

Or maybe should I just use the ruby mongodb driver in this instance?

John Nunemaker

unread,
Jul 19, 2009, 4:51:46 PM7/19/09
to mongo...@googlegroups.com
Easiest way right now would just be to have a key that is of Hash type.

class Foo
include MongoMapper::Document
key :user_options, Hash
end

Make sense?

You could also have an embedded document that was something like this:

class CustomFields
include MongoMapper::EmbeddedDocument
key :name, String
key :value, String
end

And then just have many :custom_fields on whatever thing you wanted to
allow custom values for.
Reply all
Reply to author
Forward
0 new messages