Dynamic # of Attributes for a Model (key/val table) Get/Set?

J. Riddel wrote:

I'm working on a simple product catalog. Products have user defined
attributes, such as: color, size, weight. More attributes can be added
at any time. Since these attributes are not known ahead of time, they
cannot be created as columns.

Let me suggest I a plugin I have created for this purpose.

https://rubyforge.org/projects/flex-attributes/

The plugin is still in "beta" since the application it is made for has not been rolled out to production but it seems to work well.

Basically your model looks something like:

class Product < ActiveRecord::Base
   has_flex_attributes
end

Then you can just add/remove attributes as you need. If the attribute exists on the model it will be stored there. If it does not then it will be stored in a "thin" table called product_attributes.

There are also hooks so you can define what attributes are valid. Finally the plugin works with acts_as_versioned/acts_as_versioned_association.

The docs are a little scattered right now because the product it was created for is still in development but you are welcome to use what I have.

Eric

J. Riddel wrote:

Thanks a lot. This works great. Slightly different than what I came up with (in 10 lines with MethodMissing), but it "feels" better and actually makes my relationship less complicated. Thanks a lot. Great timing.

Glad it is working for you. The plugin started out simple but I needed to also support acts_as_versioned (so the related attributes are versioned) which made the plugin much more complicated.

If you (or anybody that uses this plugin) get any problems send me a message. It is fairly new (the site it was developed for hasn't even hit production yet) so there may be some issues with it still.

Eric

Good idea, I'll take a look at it as well.