I have a Damage table that has the following fields:
- description
- date_of_damage
I'd like to add another field to store Damagepoints, which are JSON objects that look like this:
{ top: 50, left: 100 }
Top and left are the coordinates of the damagepoint on a diagram. The damagepoints are added/removed by the user using Javascript.
However, I'd like to store, in one field, an array of these damagepoints, like this:
@damage.damagepoints = [{left: 40, top: 99}, {left: 100, top: 35}, {left: 150, top: 95}]
I don't want to do this using a Damagepoint table with a has_many relationship because all of the changes to this will be done using Javascript, and if Damagepoints are created or removed by the user, I just want to pass from the client the updated array of damagepoints and replace the old one in the database with the new array. If I used a has_many relationship, I'd have to delete all of the Damagepoints and create each one new every time the array was updated (because it is too complicated and there is no benefit from deleting/adding specific damagepoints, as I don't need to track history).
What is the easiest way to store data like @damage.damagepoints (above)? All I need to do with it is pass it (via the controller) to an html5 data-attribute so that it can be used by the Javascript to add the existing damagepoints to the diagram (based on their coordinates) and then pass an updated array (from the html5 data-attribute) back to the controller via an AJAX call when the user clicks the 'Save' button.
I am using Rails 4.2 and Postresql.
Thanks!