开发者

Complex hash editing with MVC/Padrino ruby 1.8.x

I'm new to MVC. I'm using Padrino with MongoMapper and Haml to try to create this application.

I have a database of 开发者_开发技巧items, each of which has a hash associated with it called 'params'. This hash has some required keys, but mostly arbitrary/random keys. Some of the keys have a finite set of allowable values.

For example:

item.params["password"] does NOT have a finite set of possible values.

item.params["color"] must be one of %w{red blue green}

What's the best way to create an editor for the items in this hash? I'd want a text-field for free-form values and menus for finite values. I'd also like these menus to have "Create New" as the bottom choice, so I can't just hard-code all of the finite-value sets.

The best thing I can come up with for this is to have a new collection(table) in the DB that's just a Hash of default values. If your key isn't in this hash, you get a text-box.

Seems like there has to be a better way though.


Your questions seems quite similar to this one: mongodb and mongomapper

I think that you'll probably want to build a collection of "types" with "units of measure"... so your "types" collection would have entries like this:

{"_id" : "password", "display" : "password"}
{"_id" : "user_name", "display" : "text", "restrictions" : ["max_length": 20]}
{"_id" : "eye_color", "display" : ["red","blue","green"], "restrictions" : ["single"]}

So obviously, the logic for rendering this stuff will all have to be on the Ruby side. However, having tried this stuff in RDBMs, this is definitely going to be easier.

Yes the weakness here is that you could end up querying the "types" collection once for each field. So if you're showing 10 fields, you make 10 queries. It may sound a little lame, but it's basically what your MySQL Database is doing anyways. You'll probably want to build a "caching" layer for the "types" collection as you'll probably want it permanently "in-memory".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜