Pretend I have a table called books and it has a json type column called misc where some miscellaneous data has been saved. Using Rails/AREL I would like to extract just id and the number of pages (num_pages) from all the books.
In psql I can do the following:
SELECT id, misc->'num_pages' FROM books ;
id | ?column?
-------+---------------
23562 | "220"
22188 | "355"
(Note: the above query produces the same results with --> as with ->).
I expected this to work:
pry> Book.select("id, misc->>'num_pages'")
Book Load(1.7ms) SELECT id, etc-->'num_pages' FROM "books"
Book Load(1.5ms) SELECT id, etc-->'num_pages' FROM "books"
=> #<Book::ActiveRecord_Relation:0x3ff7e934d8f4>
pry> Book.select("id, misc->'num_pages'")
Book Load(1.1ms) SELECT id, etc->'num_pages' FROM "books"
=> [#<Book:0x00007fefd3c11a68 id: 23562>, #<Book:0x00007fefd3c116a8 id: 22188>]
...when I remove the ->'num_pages' part, it returns the entire misc field (so I know it's almost working):
pry> Book.select("id, misc")
Book Load(0.9ms) SELECT id, etc FROM "books"
=> [#<Book:0x00007fefe4e99fb8
id: 23562,
etc:
{"num_pages"=>"220",
...
select.