views:

20

answers:

1

I am having a Property model that should contain multiple values (just Strings). With Rails/ActiveRecord it seems that I have to create a new model (and a new table) for those values (like PropertyValue). As every one of those values just stores one String the PropertyValue only need one attribute (like value). I don't like that idea cause to access one of those values I now have to call property.values[0].value and that looks a bit ugly. Is there a nicer solution?

+1  A: 

Try serialize method

class Property < ...
  serialize :value, ::Array
end

value array will be stored as string in properties table and you can access it as normal array: property.value[3].

More details in docs.

Nikita Rybak