Search code examples
rubyjsonpostgresqlsequel

ruby sequel access json field


I have the following schema:

 Column  |            Type             |       Modifiers        
---------+-----------------------------+------------------------
 id      | text                        | not null
 data    | json                        | not null
 created | timestamp without time zone | not null default now()

If i open up psql i can run the query.

select id, data->'amount' as amount from plans;

But ruby sequel gem ruins it.

puts $db.run("select id, data->'amount' as amount from plans")

What's wrong?


Solution

  • Needed #literal method.

    $db.run("select id, data -> #{ $db.literal('amount') } as amount from plans")