Rails - Suggestions on how to store a random set of params in the DB
-
28-10-2019 - |
Question
Ok Rails gurus, I need some smart advice here.
I'm trying to use SendGrid's parse api: http://sendgrid.com/documentation/ApiParse
The big challenge here is that SendGrid does the following in the API spec:
- attachments - Number of attachments included in email.
- attachment1, attachment2, ..., attachmentN - These are file upload names, where N is the total number of attachments. For example, if the number of attachments is 0, there will be no attachment files. If the number of attachments is 3, parameters attachment1, attachment2, and attachment3 will have file uploads. TNEF files (winmail.dat) will be extracted and have any attachments posted.
I need to store the response in the DB so I can send it off to delayed job to do all the heavy lifting.
What's the best way to store this so delayed job can use the response? Can I somehow turn the response into a Mail.new() object? Or some other ideas? The random X of attachments makes it tricky to solve elegantly without add 100 fields to the database.
Thanks
Solution
I would try serializing the attachments and storing in a single field. I don't think recreating a Mail object will do you much good.
Go here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html and look for this title "Saving arrays, hashes, and other non-mappable objects in text columns"
OTHER TIPS
If I understood right I suggest following:
If you do not need query upon the fields, you chould store the attributes hash as YAML or JSON into one field in database table.