简体   繁体   中英

Batch update array field using Ruby Active Record

So let's say I have a model that has an array.

In some cases, I want to be able to do a batch update to add something to that array.

eg

Parent is a model of {id: int, first_name: string, last_name: string, jobs :Array[string]}

and I want to update many Parents to have one extra job.

Is there a way to do this with one command? Or do I have to go through every row in the Parent table and append?

You can use serialize method

For eg: in model you can write as follows

serialize :<FIELD_NAME>, Array

And you can use group of checkboxes or radio buttons to store them. It will store as Array.

Try it out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM