rails-使用多个更改的属性更新活动记录关系 [英] rails - updating active record relation with multiple changed properties
问题描述
我有一个活动记录关系,定义如下:
I have an active record relation defined like this:
contacts = Contact.where("status = 'waiting'")
然后我运行 .each
循环并根据某些逻辑更改联系人的属性。
I then run an .each
loop and change properties of contacts depending on some logic.
我不想对每个对象进行保存!
个人联系人...循环后如何更好地保存整个联系人关系?
I don't want to do a save!
on each individual contact... how can I better save the entire contacts relation after the loop?
contacts.each do |contact|
//Change properties of contact here
end
在包含新属性后,我该如何保存新更新的联系人activerecord关系?
How do I save the newly updates contacts activerecord relation after it contains its new properties?
最有效的方式不能通过联系人保存来进行联系。
The most efficient way can't be doing a contact by contact save.
推荐答案
update_all可以使您免于执行循环并为您完成保存,这里是文档。
https://apidock.com/rails/v4.0.2/ ActiveRecord / Relation / update_all
update_all saves you from doing the loop and do the save for you, here is the documentation. https://apidock.com/rails/v4.0.2/ActiveRecord/Relation/update_all
这篇关于rails-使用多个更改的属性更新活动记录关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!