简体   繁体   中英

Rails - update multiple active record relations

I am doing an update all to a Model to update several ActiceRecord Relations.

But I want to skip an attribute if its not provided (if its nil or empty I dont want to update that attribute)

   def update_numbers(numbers, comment)
      numbers.update_all (number_status: Number::STATUS_UPDATED, comment: comment if void_comment.present?)
    end

This obviously does not work as it doesn't like the if condition. Is there a way to not try and update comment if its .blank?

Does this work for you?

def update_numbers(numbers, comment)
  hash = { number_status: Number::STATUS_UPDATED }
  hash[:comment] = comment if comment.present?

  numbers.update_all hash
end

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