The following data is present in my Color ActiveRecord model:
| id | colored_things |
|---|---|
| 1 | [{"thing" : "cup", "color": "red"}, {"thing" : "car", "color": "blue"}] |
| 2 | [{"thing" : "other", "color": "green"}, {"thing" : "tile", "color": "reddish"}] |
| 3 | [{"thing" : "unknown", "color": "red"}] |
| 4 | [{"thing" : "basket", "color": "velvet or red or purple"}] |
The colored_things column is defined as jsonb.
I am trying to search on all "color" keys to get values that are like a certain search term. This SQL query (see SQL Fiddle) does that:
SELECT DISTINCT C.*
FROM colors AS C,
jsonb_array_elements(colored_things) AS colorvalues(colorvalue)
WHERE colorvalue->>'color' ILIKE '%pur%';
Now I would love to translate this query to a proper Active Record query, but the below does not work:
Color.joins(jsonb_array_elements(colored_things) AS colorvalues(colorvalue))
.where("colorvalue->>'color' ILIKE '%?%'", some_search_term)
.distinct
This gives me the error:
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: invalid reference to FROM-clause entry for table "colors")
Can someone point me in the proper direction?