1

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. Commented May 13, 2011 at 19:37

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.