How can I save a ruby hash directly to postgres using sql?
I've tried like this :
INSERT INTO "table" ("created_at", "key", "params") VALUES (now(), 'create', '{:sub => tralal}')
But it inserts the string into the database, how can I insert a hash like I do from ruby when I save the object.
The sql query who performs insert from rails looks like :
INSERT INTO "table" ("created_at", "key", "params") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Mon, 24 Feb 2014 18:53:58 UTC +00:00], ["key", "create"], ["parameters", "---\n:sub: tralal\n"]
I'm trying to do some inserts with sql for performance gains, but I can't figure out how to put a hash in db instead of string
If I use the converstion to yaml I get:
{:my => 'hash'}.to_yaml
=> "---\n:my: hash\n"
And when I do :
INSERT INTO "table" ("created_at", "key", "params") VALUES (now(), 'create', '---\n:my: hash\n')
Then when I try to get it back in the rails console :
MyClass.last[:parameters].keys.first
=> "---\\n:my"
I get ---\\n:my instead of :my