1
votes

I want to model e-commerce product attributes that can be changed in run-time.

So for example, on the Create Product page, the user adds a certain product with attributes: color, size. And then adds some other product with different attributes: resolution, diagonal size. Basically the user is able to define new attributes at run-time.

How do I handle variable attributes in the product model? I'm used to relational databases where the fields of each table are defined a priori.


Edit: I'm using Mongoid. Let me be more specific. Let's say I have this product model:

class Product
  include Mongoid::Document
  field :title
  field :description
  field :price
# attributes not known yet ???
end

The other fields of the product are clearly defined in the model, but not the attributes. Perhaps Mongoid/MongoDB doesn't care whether they are defined in the model or not, and just adds them in the document?

1
Updated my answer for Mongoid.Mario

1 Answers

1
votes

It's done for you. It just works. Add the attributes and save them. How it's done depends on what adapter you're using.

Here's a simple guide to setting it up from MongoDB.org. And this page has documentation you'd need if you're using MongoMapper.

Using Mongoid? Again, just set attributes (you'll have to set and access them like a hash: use []= and []), and save it. Dynamic fields!