Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails where clause when something is stored as array

I am running rails 4.2, with a PG database.

I have an item stored in the database such as (model Item):

:something => ["1", "2", "3"]

I would like to get the Item.where(:something.include? => "3")

Obviously this is not working - but how are you meant to do this in rails?

like image 765
Jmohey Avatar asked Mar 09 '16 14:03

Jmohey


1 Answers

According to documentation, something like this should work:

Item.where('something @> ARRAY[?]::varchar[]', ['3'])
like image 87
potashin Avatar answered Sep 28 '22 12:09

potashin