Search code examples
ruby-on-railsactiverecordupdate-all

How to optimize update_all calls


Currently, I'm doing the update_all with concatenation like this:

collection.update_all(flag: false)
collection.update_all(["description = CONCAT(description, ?)", 'not_available'])

Is it possible to do it at once?

Obviously, this construction doesn't work:

collection.udpate_all(flag: false, ["description = CONCAT(description, ?)", 'not_available'])

Solution

  • update_all can take array, so you should be able to do something like this:

    collection.udpate_all([
      "description = CONCAT(description, ?)", flag = ?,
      'not_available',
      false
    ])