ruby iterate through sql results -


i need iterate through records found sql , update them all.

i need update couple of thousand records , requirements complex enough search records sql. code below works there way of doing without performing several thousand update statements?

users = user.find_by_sql "select e.id users e inner join accounts on a.id = e.account_id e.account_id not in (1955,3083, 3869)" users.each |user|   u = user.find(user.id)   if u     u.update_attribute(:last_name, 'x')   end end 

this same thing code, single update:

user.joins(:accounts)   .where("users.account_id not in (?)", [1955, 3083, 3869])   .update_all(last_name: "x") 

you can find documentation update_all here.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -