铁轨上的红宝石。堆栈级别太深错误

时间:2015-08-17 07:53:06

标签: ruby-on-rails model

堆栈级别太深错误导致我的应用程序崩溃。 我有一个存款模型有两行:收入和提款。 这两行由收入和提现模型中的after_create回调修改。 在提款模型中,我想使用before_create回调来验证存款余额(存款余额必须在创建提款后大于或等于0)并收到错误: CashoutsController中的SystemStackError #create。

ps:收入回调工作正常。

代码:

deposit.rb

my = Mysql.new('localhost', 'root', '', 'videos') 

list = my.query('select * from offline_renditions where secure_url LIKE "http://abcd.testing.com/movies/%" and name != "tiny"')

list.each_hash do |a| 
  ref = a['secure_url'].match(/^http:\/\/data.myDB.com\/([^\/]+)\/([^\/]+)\/([^\/]+)/)[2]
end

schema.rb

...

class Deposit < ActiveRecord::Base
    belongs_to :user
    has_many :incomes
    has_many :cashouts
end

cashout.rb

  create_table "deposits", force: true do |t|
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.decimal  "income",     precision: 12, scale: 2
    t.decimal  "cashout",    precision: 12, scale: 2, default: 0.0
  end

cashouts_controller.rb

class Cashout < ActiveRecord::Base
    belongs_to :deposit
    validates :deposit_id, :order_sum, presence: true
    validates :order_sum, numericality: true
    validates :order_sum, numericality: { greater_than_or_equal_to: 0}
    before_create :validate_order #before_save crashed too
    after_create :update_deposit_cashout # working fine

    private

    def validate_order
        @deposit = self.deposit
        @income = @deposit.income
        @cashout = self.order_sum
        if @income - @cashout >= 0
            self.save
        else
            !self.save
        end
    end

    def update_deposit_cashout
        @deposit = self.deposit
        @deposit.update_attributes(:cashout => @cashout + self.order_sum)
    end
end

income.rb

class CashoutsController < ApplicationController
  before_action :signed_in_user, only: [:create]

  def new
    @cashout = @deposit.cashouts.build
  end

    def create
    @deposit = current_user.deposit
    @cashout = @deposit.cashouts.build(cashout_params)
    @cashout.save 
    if @cashout.save
      flash[:success] = "Your order request has been sent!"
      redirect_to '/deposit'
    else
      flash[:error] = "Your order request hasn't been sent!"
      redirect_to '/deposit'
    end
  end

  def show
    @deposit = Deposit.find(params[:id])
    @deposit.cashout
  end


  private

    def cashout_params
      params.require(:cashout).permit(:order_sum)
    end

end

incomes_controller.rb

class Income < ActiveRecord::Base
    belongs_to :deposit
    validates :deposit_id, :order_sum, presence: true
    validates :order_sum, numericality: true
    validates :order_sum, numericality: { greater_than_or_equal_to: 0}

    after_create :update_deposit_income

    private

    def update_deposit_income
        @deposit = self.deposit
        if @deposit.income == nil
            @income = 0
        else
            @income = @deposit.income
        end
        @deposit.update_attributes(:income => @income + self.order_sum)
    end
end

我的错误在哪里?

1 个答案:

答案 0 :(得分:4)

stack level too deep错误通常会发生,因为你的代码卡在一个循环中:一个方法调用自身,或两个方法反复调用。

在你的情况下,我认为它是由validate_order方法引起的。你在before_save回调中调用它,方法调用self.save,它将触发回调,它将再次调用该方法,这将再次保存,这将触发回调等等。 / p>

暂时考虑一下 - 将对象保存为“before_save”功能的一部分是没有意义的吗?

我怀疑你想要做的只是验证对象,如果验证失败,那么无论如何都会自动阻止保存。像这样的东西

validate :validate_order

def validate_order
  @deposit = self.deposit
  @income = @deposit.income
  @cashout = self.order_sum
  if @income - @cashout <= 0
    self.errors.add(:deposit, "is too small")
  end
end

这只是一个例子,因为我不知道你所测试的实际逻辑应该是什么。