samedi 25 avril 2015

Rails update multiple columns based on an array


I'm trying to blank out a bunch of columns in an easy-to-read formatted code. I know I could just do this:

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

But that doesn't seem like a very Ruby way to do things, nor is it particularly clean to read.

I'm trying to figure out why I can't just do an 'each do' array like 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 do |record|
  @user.record = nil
end

@user.save

I receive the following error:

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

I know that similar questions have been asked before, but they are usually related to updated a bunch of different tables. I'm only interested in the user table.

Also, there are a lot of answers linking to http://ift.tt/1apioMa. But that's an old deprecated method that apparently bypasses callbacks. That seems rather dangerous.

Can anyone think why a simple 'each do' array won't work?


Aucun commentaire:

Enregistrer un commentaire