简体   繁体   English

Ruby on Rails抽佣任务未返回最新模型信息

[英]Ruby on rails rake task not returning up-to-date model info

While building a billing system I encountered the following problem: 在构建计费系统时,我遇到了以下问题:

In scheduler.rake : 在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. 这段代码应该在每次运行rake任务时不断增加,除非总数达到> 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 上面的代码始终会显示“最后发票= 100” =>每次运行rake任务时,它应该增加

Every new invoice still has the same total_price_in_cents (when I'm expecting that it should increment). 每张新发票仍具有相同的total_price_in_cents(当我期望应该增加时)。

What is going on ? 到底是怎么回事 ?

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. 在更新的代码中,看起来您在调用user.invoices.new之后正在调用user.invoices.last,这就是为什么它总是返回相同值的原因。

Create a variable @last_invoice = user.invoices.last before call user.invoices.new. 在调用user.invoices.new之前,创建一个变量@last_invoice = user.invoices.last。

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. 在您的原始代码发布中,您对@invoicesave调用@invoice发生在循环之外-我相信您只保存了一次。

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 因此,虽然循环了users表,但从不保存更新-退出循环后的最后一次除外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM