Is there a way to restrict what hstore columns can be saved? I've got below code for doing this so far:

store_accessor :widget_locations, :left_area1, :mid_area1, :left_area2, :mid_area2, :right_area2

but this seems to still allow other key names to be saved ie. middle_area123

Also how am I able to update hstore like update_attributes or update?

Question author Passionate-developer | Source



I could be wrong but my guess is that you are making calls on widget_locations like

item.widget_locations[:left_area1] = thing

If so, you should change that to

item.left_area1 = thing

because you told the store_accessor to create attributes :left_area1, :mid_area1, :left_area2, :mid_area2, :right_area2 that will be serialized to database column :widget_locations. Now these attributes will behave like normal attributes, so you can put validations on them etc.

This also allows you to update an item as usual:

item.update(name: 'Test', left_area: 'garden', mid_area: 'livingroom')

The catch with a hstore is that accessing the serialized column will allow you to add new unknown attributes, so it is best to directly access the attributes you explicitly specified.

Answer author Zwippie

Ask about this question here!