How to update the value of an array field using the add function

In the database, I have an is_deleted array is_deleted with a serialization function in the model. I want to add array values ​​to a database field. Here is the function used in postgresql.

 Message.update_all(['is_deleted = array_append(is_deleted, ?)', 2]) 

How to do this with sqlite database?

+5
source share
1 answer

you need serialize column in model

 Class Message < ActiveRecord::Base serialize :is_deleted, Array end 

for an extra note, if you want a column to be used to store an array, then it should be a column_type or text string

 Message.all.each { |m| m.update_attribute(:is_deleted, m.is_deleted.push(2)) } 
+1
source

Source: https://habr.com/ru/post/1273231/


All Articles