I have a child model, which should be able to store different attributes via the ActiveRecord::Store feature. Those attributes should be determined by the parent model. For this purpose, the parent model has a column content_attributes
which stores the children's attributes as array of strings (i.e. ['color', 'size', 'age']
).
To have accessors in the child instances for all attributes defined by the parent, i currently use a workaround which maps all attribute names of all available parents:
class child
belongs_to :parent
store :content, accessors: Parent.all_content_attributes, coder: JSON
...
end
Actually, i only want to set accessor's for all attributes of the distinct parent. However, in the example above, the child instance would get a long list of dispensable attribute names. How to replace the Parent.all_content_attributes
? Guess i need some kind of metaprogramming here?!