Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:59:50+00:00 2026-06-04T06:59:50+00:00

While building a billing system I encountered the following problem: In scheduler.rake : @users.each

  • 0

While building a billing system I encountered the following problem:

In scheduler.rake :

@users.each do |user|
begin
  puts "CALCULATING COSTS =========="
  costs = user.total_billable_price_per_month(Time.now)
  lead_count = user.total_billable_leads_count(Time.now)
  click_count = user.total_billable_clicks_count(Time.now)

  puts "CREATING NEW INVOICES ======="
  @invoice = user.invoices.new

  # if user.free_credits
  #         invoice.total_price_in_cents = 0
  #       else
  #         invoice.total_price_in_cents = costs.cents
  #       end



  @invoice.total_leads = lead_count
  @invoice.total_clicks = click_count
  @invoice.total_price_in_cents = costs.cents

  # Als de vorige factuur onder de 10 euro was, dan geld bij huidige factuur optellen
  if user.invoices.last && user.invoices.last.total_price_in_cents < 1000
    puts "Bedrag onder 10 euro"
    puts "Last invoice = #{user.invoices.last.total_price_in_cents}"
    @invoice.total_price_in_cents += user.invoices.last.total_price_in_cents
  end

  puts "SAVING INVOICE FOR #{user.name} with ID = #{user.id}"
  # Factuur saven
  @invoice.save

  #Als de factuur hoger of gelijk is als 10euro, dan factuur aanmaken
  if @invoice.total_price_in_cents >= 1000
    #Moneybird factuur versturen
    puts "COSTS ARE HIGHER THAN 10 EURO, CREATING MONEYBIRD INVOICE"

    moneybird_invoice = MoneybirdInvoice.new
    sleep(15)
    moneybird_invoice.contact_id = MoneybirdContact.find_by_customer_id(user.id).id
    sleep(15)
    moneybird_invoice.details_attributes = [
         { :description => "Aantal leads", :amount => lead_count, :price => user.lead_price.cents.to_f/100},
         { :description => "Aantal clicks", :amount => click_count, :price => user.click_price.cents.to_f/100}
    ]

    puts "TRYING TO SAVE MONEYBIRD INVOICE"
    if moneybird_invoice.save && moneybird_invoice.put(:send_invoice)
      puts "SUCCESFULLY SAVED INVOICE"
      #GET UPDATED PAY URL
      @sent_mb_invoice =  MoneybirdInvoice.get(moneybird_invoice.id)
      sleep(15)
      @invoice.update_attributes(:moneybird_invoice_id => @sent_mb_invoice['invoice_id'], :moneybird_pay_url => @sent_mb_invoice['pay_url'])
    else
      puts "MONEYBIRD INVOICE FAILED"
      puts moneybird_invoice.errors.inspect
    end
  else
    # GEEN MONEYBIRD FACTUUR AANMAKEN
  end

rescue
  puts "ER IS IETS FOUT GEGAAN MET FACTUREREN USER ID = #{user.id} & NAME = #{user.name}"
  puts @invoice.errors.inspect
end
end

This piece of code should constantly increment each time the rake task is run, except when the total amount reaches > 1000.

if user.invoices.last && user.invoices.last.total_price_in_cents < 1000
  puts "Bedrag onder 10 euro"
  puts "Last invoice = #{user.invoices.last.total_price_in_cents}"
  @invoice.total_price_in_cents += user.invoices.last.total_price_in_cents
end

The code above always puts “Last invoice = 100” => This should increment each time the rake tasks is run

Every new invoice still has the same total_price_in_cents (when I’m expecting that it should increment).

What is going on ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:59:51+00:00Added an answer on June 4, 2026 at 6:59 am

    EDIT: Added after code upadte:

    In your updated code, it looks like you were calling user.invoices.last after you called user.invoices.new, this is why it always returned the same value.

    Create a variable @last_invoice = user.invoices.last before call user.invoices.new.

    ORIGINAL ANSWER:

    In your original code posting, it looks like your save call on @invoice happened outside the loop — I believe you’re only saving it once.

    task :create_invoices => :environment do  
    
      # Begin the loop for each use
      User.all.each do |user|
        @invoice = user.invoices.build
        #If last bill < 1000
        if user.invoices.last && user.invoices.last.total_price_in_cents < 1000
    
          puts "Last invoice = #{user.invoices.last.total_price_in_cents}"
          @invoice.total_price_in_cents += user.invoices.last.total_price_in_cents
          @invoice.save
    
        end # end 'if' statement
      end # end loop for all users
    end # end task definition
    

    So you loop though the users table, but never save updates — except for the very last time after you exit the loop

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While building my application with relational tables I ran into the following problem :
While building several different projects in QtCreator, I have run across the following build
While building a JAR file (successfully) in Netbeans 7.1.1 , I encountered this warning:
I came across with these situation while building a class diagram for a system,
I have done simple java app for blackberry, while building am getting following error.
While building my assembler for the x86 platform I encountered some problems with encoding
I am getting following error while building my vc++ project (Using visual studio 2010)
I'm following the NerdDinner tutorial while building my own application. The tutorial gives this:
I came in touch with a little problem while building a model with a
I used to have the following rule in my .htaccess while building a website:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.