One of the awesome features of MongoDB is the ability to store arrays directly in your documents, which maps directly with ruby/python/js arrays etc.
An example Post model:
1 2 3 4 |
|
Though when working with MongoDB arrays in rails and forms to edit the data, you could end up doing a bunch of controller logic and/or js logic just to get the data in.
This is especially true if you want to just store a bunch of strings in the array. I tried many different ways of doing this, but the simplest solution with out any changes to your controller or forms is to just use a virtual attribute on your model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Then in your view you just use tags_list:
1
|
|
In the shell:
1 2 3 4 5 6 |
|
Any comma seperated values entered will be stored correctly in MongoDB, you could also use this to apply any transformations to the data before it saved, i.e captilizing each value.
We ended up using this bit of code quite often, so i extracted it out to lib, and then packaged it into a gem.