Search code examples
ruby-on-railsactiverecordentity-attribute-value

Unlimited arbitrary properties (key/value pairs) for an ActiveRecord model


Using ruby on rails, I have a Customer table that I want to be able to add unlimited properties (key value pairs) to. I'm not sure what the key/value pairs will be yet so I'm not sure how to do this. For example, one customer could be:

  • Customer 1 properties:
    • color: 'yellow'
    • brand: 'nike'
    • sales: '33'
  • Customer 2 properties:
    • color: 'red'
    • phone_number: '1111111111'
    • purchases: '2'

Basically, customers can have any number of properties in a key/value pair.

How can I do this?


Solution

  • You should be able to use serialize for this, and assign your properties hash to your properties attribute, and retrieve them in the same way.