Roman Kanafotskiy
Roman Kanafotskiy

Reputation: 89

Rails numerate records

I have a list of records with number attribute.

My desired result is:

Tried:

    Invoice.all.each do |i|
     i.number = "#{sprintf '%03d', i.user.invoices.count + 1}"
     i.save
    end

But got the current result:

How do I get the desired result? Thanks

Upvotes: 1

Views: 46

Answers (1)

Muhammad Faisal Iqbal
Muhammad Faisal Iqbal

Reputation: 1836

try something like

    User.all.each do |user|
      number = 1
      user.invoices.order(:id).each do |i|
       i.number = "#{sprintf '%03d', number}"
       i.save
       number +=1
      end
    end

Upvotes: 1

Related Questions