ruby - Rails update multiple columns based on an array -


i'm trying blank out bunch of columns in easy-to-read formatted code. know this:

@user = user.find(3) @user.column1 = nil @user.column2 = nil @user.column3 = nil etc... 

but doesn't seem ruby way things, nor particularly clean read.

i'm trying figure out why can't 'each do' array this:

columns = [ "key", "provider", "uid", "access_code",                 "customer_id", "cc_id", "cc_brand", "cc_last4",                 "cc_expiration", "last_payment_id", "last_payment_date",                 "last_payment_amount" ]  columns.each |record|   @user.record = nil end  @user.save 

i receive following error:

undefined method `record=' #<user:0x00000003a91d18> 

i know similar questions have been asked before, related updated bunch of different tables. i'm interested in user table.

also, there lot of answers linking http://apidock.com/rails/activerecord/base/update_all/class. that's old deprecated method apparently bypasses callbacks. seems rather dangerous.

can think why simple 'each do' array won't work?

you can use send funtion dynamic functions names:

 @user.send("#{record}=", nil) 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -