0

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?

1
  • Can you not hit the UserPreference object directly here, maybe UserPreference.where(...).update_all(...) Commented Apr 2, 2019 at 15:15

1 Answer 1

1

I can't test it right now, but you could use joins:

UserPreference.joins(:employer).update_all('send_email = employers.send_emails')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.