Querying a Postgres array of integers in Rails -


my model has pg array use store integers

querying using methods found other questions yields errors or gives empty results

mymodel.where("? = (myarray)", 42) 

gives

pg::undefinedfunction: error:  operator not exist: integer = text 

and

 mymodel.where("myarray @> '{?}'", 42) 

gives empty results, yet have model 42 1 of ints in array

#<mymodel:0x007f9a77dd5608> {                 :id => 170,       :myarray => [     [0] 42,     [1] 43,     [2] 58,     [3] 61,     [4] 63   ] 

is there special way query integers(or floats) within postgres array in rails?

the migration

class addmyarraytomymodel < activerecord::migration   def change     add_column :my_models, :myarray, :integer, array: true, default: []     add_index  :my_models, :myarray, using: 'gin'   end end 

and schema

t.integer  "myarray",                 default: [],              array: true 

try these:

mymodel.where("? = any(myarray)", '{42}') 

or

mymodel.where("myarray @> ?", '{42}') 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -