Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to populate column_name with items from @enc. Problem is #update_all updates all fields with last item in array?

Last item is "MPq3KSzDzLvTeYh+h00HD+5FAgKoNksykJhzROVZWbIJ36WNoBgkSoicJ5wx\nog0g\n".

I am trying to populate with all items from array not just last. I hope question is clear? I tried #update_attributes, but no success? Help. Thanks

@enc=["hUt7ocoih//kFpgEizBowBAdxqqbGV1jkKVipVJwJnPGoPtTN16ZAJvW9tsi\n3inn\n", "wGNyaoEZ09jSg+/IclWFGAXzwz5lXLxJTUKqCFIiOy3ZXRgdwFUsNf/75R2V\nZm83\n", "MPq3KSzDzLvTeYh+h00HD+5FAgKoNksykJhzROVZWbIJ36WNoBgkSoicJ5wx\nog0g\n"]

@enc.each do |i|
PaymentMethod.update_all(enc_number: i)
end
share|improve this question

closed as unclear what you're asking by Marek Lipka, Pavan, Stefan, Зелёный, zishe Jul 27 '14 at 10:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

    
Could you replace Model and column_name with your actual model and column names? –  Stefan Jul 25 '14 at 9:31
    
@ashwinkumar I am using rails 3.2 so using update_attributes gives error: undefined method –  user3566065 Jul 25 '14 at 9:33
    
@Stefan I dont get what u mean? –  user3566065 Jul 25 '14 at 9:33
    
Your model probably isn't called Model and column_name isn't the actual column's name –  Stefan Jul 25 '14 at 9:34
    
@Stefan I just didnt see how that would help? –  user3566065 Jul 25 '14 at 9:37

1 Answer 1

up vote 0 down vote accepted
  PaymentMethod.all.each_with_index do |payment, n = 0|
   payment.update_column(:enc_number, @enc[n])
   n +=1
  end
share|improve this answer
    
I think this code is working but not updating to database? How do I update results to database? –  user3566065 Jul 25 '14 at 9:59
    
What do you mean not updating? and code is working? Is it not updating the fields? Try this "payment.update_attributes(enc_number: @enc[n])" –  ashwin kumar Jul 25 '14 at 10:02
    
no sorry it just selects all from PaymentMethod..thats it..even with "payment.update_attributes(enc_number: @enc[n])" Thanks 4 trying –  user3566065 Jul 25 '14 at 10:06
    
try removing statement "next unless @env.size < n" –  ashwin kumar Jul 25 '14 at 10:10
    
@user3566065 Did it worked? –  ashwin kumar Jul 25 '14 at 10:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.