Storage of Compound Data Type

Hi folks.

I need to store an array. I need to use it later, but want to store it
into a field in a record for now. Here’s the original:

a = [1, 2, 3, ["alpha", "bravo"], "six", 8.55]

Can I somehow do this?

require 'json'
b = a.to_json

Can I then shove b into the database through activerecord? I’ve been
trying that and it doesn’t seem to work. All other fields are working:

r = Request.new
r.req_date = DateTime.now
r.username = params[‘username’]
r.email = params[‘email’]
r.description = params[‘description’]
r.request = params.to_json
r.save

Any advice appreciated. Cheers

BeeRich L. wrote in post #1167312:

Can I then shove b into the database through activerecord? I’ve been
trying that and it doesn’t seem to work. All other fields are working:

ActiveRecord has the concept of serializers. In your model you can
write:

serialize(:request, JSON)

request here being your database column.

Now it’ll automatically serialize the JSON data you give it. It’ll call
dump on it before saving it, and loading it with load before handing
it JSON blob to you.

Hi Mads. Thanks for the reply.

This is a Sinatra application, so no model. So that is just a method
then? I’ll serialize it before inserting into the table.

When I grab it, how do I unserialize it? “unserialize”?

Thanks for the notes.

Cheers