squeel icon indicating copy to clipboard operation
squeel copied to clipboard

Convert column

Open eugenio-caicedo opened this issue 6 years ago • 1 comments

I have a column of type String in the User model, this column is an array that is saved as a string. I need to check if there is an element in that array with the where clause. How can I do it?

I thought something like this:

scope :notified, ->(user){ where{array(tags).in [user.id] } }

But it does not work.

eugenio-caicedo avatar Aug 02 '17 14:08 eugenio-caicedo

This did not work, but it was by the generated sql order in the clause where.

scope :notified, ->(user){ where{user.id == any(cast(tags.as 'int[]')) } }

SQL: SELECT "users".* FROM "users" WHERE any(cast("users"."tags" AS int[])) = 1

eugenio-caicedo avatar Aug 02 '17 15:08 eugenio-caicedo