简体   繁体   中英

Update nested records in one sql query (active record, ruby on rails)

I'd like to update all user preferences to a default value efficiently. The following works correctly but does not seem good enough:

Employer.find_each do |employer|
  employer.user_preferences.update_all(send_emails: employer.send_emails)
end

Any hints?

我现在无法对其进行测试,但是您可以使用joins

UserPreference.joins(:employer).update_all('send_email = employers.send_emails')

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