ruby - appending to rails field value -


i need find , update number of records in rails 3.2, ruby 2 application. following code finds records want. need though add " x" (including space) email address of every user , can't figure out how it.

this finds records

user.joins(:account)   .where("users.account_id not in (?)", [1955, 3083, 3869])   .where("accounts.partner_id in (?)", [23,50])   .where("users.staff = '0'")   .where("users.admin = '0'")   .where("users.api_user = '0'")   .where("users.partner_id null")   .update_all(email: :email.to_s << " x") 

but it's last line i'm having problems with. possible, or need find records way?

the update_all method updates collection of records, unless write own sql expression, can set 1 value. example, if wanted overwrite email addresses "x", easily:

user.joins(:account)   .where("users.account_id not in (?)", [1955, 3083, 3869])   # ...other scopes...   .update_all(email: "x") 

in case, need make individual updates these records. 1 way find records, loop on them , update them 1 @ time:

users_to_update = user.joins(:account)                       .where("users.account_id not in (?)", [1955, 3083, 3869])                       .where("accounts.partner_id in (?)", [23,50])                       .where("users.staff = '0'")                       .where("users.admin = '0'")                       .where("users.api_user = '0'")                       .where("users.partner_id null")  users_to_update.each |user|   user.update_attribute(:email, "#{user.email} x") end 

another solution use sql expression update_all, in zoran's answer.


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 -