Reputation: 16241
Forgive me if this has been answered, I have spent a while going through similar questions but none of them appear to achieve what I'm looking for.
I have a model named Contact
which stores information for a.. contact. I would like to add the ability to add custom fields. That is, a user can store a key
and value
which is stored in a serialized object on the model. Because the key is custom I can't just use attr writers to access the values as if they were on the model itself.
I have tried serializing this as a Hash but my problem is using form inputs for when submitting the data. For this reason I started using an Array of Hashes with [{key: 'foo', value: 'bar'}] but again, when editing the form the old values wont show up and I'm not entirely sure of the best way to approach this. I was trying to achieve this with fields_for :extra_values
Upvotes: 3
Views: 664
Reputation: 5294
There are some small problems with storing data in serialized fields in model. First, ActiveRecord always consider serialized fields dirty, as a result, the record in database will always be updated whenever save
is called. Second, it is difficult to search the data saved in serialized fields. So I suggest adding another model ExtraValue to store those values, and making Contact has_many :extra_values
. Then you can take advantage of nested forms to build your form.
Upvotes: 4