Home > database >  rails console no method each for model class
rails console no method each for model class

Time:12-19

Why should this happen:

Sms.each do |sms|
  sms.time_stamp=Time.at(sms.time_stamp_long/1000)
  sms.save
end

undefined method `each' for Sms:Class (NoMethodError)

CodePudding user response:

It's better to use find_each

Sms.find_each do |sms|
  sms.update_columns(time_stamp: Time.at(sms.time_stamp_long / 1000)
end

CodePudding user response:

looks like you are trying to call each on the Sms class itself, rather than on an instance of Sms or a collection of Sms objects try this

Sms.all.each do |sms|
  sms.time_stamp = Time.at(sms.time_stamp_long/1000)
  sms.save
end```
  • Related