Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 当某个模型中没有记录时,rails会创建两次 猜测_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 当某个模型中没有记录时,rails会创建两次 猜测

Ruby on rails 当某个模型中没有记录时,rails会创建两次 猜测,ruby-on-rails,ruby,Ruby On Rails,Ruby,奇怪的是,当数据库中没有记录时,它肯定会发生 我试过的 我怀疑真正的原因是ajax,所以我全部删除了remote:true,但它几乎不起作用。 控制器 def create @sell = current_user.orders.build(sell_params) @buy = current_user.orders.build(buy_params) if @sell.save flash[:success] = 'your order has successfully

奇怪的是,当数据库中没有记录时,它肯定会发生

我试过的 我怀疑真正的原因是ajax,所以我全部删除了
remote:true
,但它几乎不起作用。 控制器

def create
  @sell = current_user.orders.build(sell_params)
  @buy = current_user.orders.build(buy_params)
  if @sell.save
    flash[:success] = 'your order has successfully submitted.'
    notify_order(@sell.rate, @sell.amount, @sell.order_type)

    @sell.rate.present? ? @sell.update(order_type: 'limit_sell') : @sell.update(order_type: 'market_sell')

    fund = Fund.create_with(amount: 0, crypto_address_id: 1)
            .find_or_create_by(user_id: current_user.id, kind: @sell.pair.split('_').last)
    fund.update(in_use: @sell.rate * @sell.amount) if @sell.order_type == 'limit_sell'
    # try to make orders done
    market_checker(@sell.pair)
  else
    if Order.where(order_status: 'done', pair: params[:order][:pair]).present?
      @currency_price = Sell.where(trading_status: 'done', currency_id: Currency.find_by_slug(params[:sell][:currency_id]).id)
      @currency_price.present? ? @currency_price = @currency_price.last.price : @currency_price = ''
    else
      flash[:danger] = "Unfortunately, there is no trading right now so that we can't show you the chart"
    end
    @pair = params[:order][:pair].to_s
    render :new
  end

  if @buy.save
    flash[:success] = 'your order has successfully submitted.'

    notify_order(@buy.rate, @buy.amount, @buy.order_type)

    @buy.rate.present? ? @buy.update(order_type: 'limit_buy') : @buy.update(order_type: 'market_buy')

    fund = Fund.create_with(amount: 0, crypto_address_id: 1).find_or_create_by(user_id: current_user.id, kind: @buy.pair.split('_').last)
    fund.update(in_use: @buy.rate * @buy.amount) if @buy.order_type == 'limit_buy'

    # try to make orders done
    market_checker(@buy.pair)
  else
    if Order.where(order_status: 'done', pair: params[:order][:pair]).present?
      @currency_price = Sell.where(trading_status: 'done', currency_id: Currency.find_by_slug(params[:sell][:currency_id]).id)
      @currency_price.present? ? @currency_price = @currency_price.last.price : @currency_price = ''
    else
      flash[:danger] = "Unfortunately, there is no trading right now so that we can't show you the chart"
    end
    @pair = params[:order][:pair].to_s
    render :new
  end
end
模型

class Order < ApplicationRecord
  belongs_to :user
  validate :deposited_btc_enough?
  validate :insufficient?
  validates :amount, format: { with: /\A\d+(?:\.\d{0,8})?\z/ }, numericality: { greater_than: 0.000000009, less_than: 100_000_000 }, presence: true
  validate :rate_check
  validates :rate, format: { with: /\A\d+(?:\.\d{0,8})?\z/ }, numericality: { greater_than: 0.000000009, less_than: 100_000_000 }, if: :rate_present?
  validate :checking_order_type

  def rate_present?
    rate.present?
  end

  def checking_order_type
    if order_type == 'sell_limit' || order_type == 'buy_limit'
      errors.add(:Please, 'specify the rate of your order.') unless rate.present?
    end
    if order_type == 'sell_market' || order_type == 'buy_market'
      errors.add(:You, "can't specify the rate of your order.") if rate.present?
    end
  end

  def deposited_btc_enough?
    if rate.present?
      if rate.to_d > '0'.to_d && amount > 0
        deposited_amount = user.fund.amount if user.fund.amount.present?
        amount = rate.to_d * amount.to_s.to_d
        if deposited_amount.present?
          if coin_to_satoshi(amount).to_i > coin_to_satoshi(deposited_amount).to_i
            errors.add(:Your, 'deposited yen is short to order.')
          end
        else
          errors.add(:You, "haven't deposited yen  yet or Your transaction hasn't confirmed yet.")
        end
      else
        errors.add(:You, "can't specify negative numbers here.")
      end
    end
  end

  def to_param
    uuid
  end

  def insufficient?
    if amount.present? && order_type.split('_').last == 'sell'
      if CurrencyUser.find_by(user_id: user.id, currency_id: Pair.find_by(name: pair).currency_id).amount < amount
        errors.add(:amount, 'of this order is more than your holdings.')
        errors[:base] << 'Errors that are related to this order exist.'
      end
    end
  end

  def is_number?(string)
    true if Float(string)
  rescue StandardError
    false
  end

  def rate_check
    if rate.present?
      errors.add(:You, 'can only specify numbers') unless is_number?(rate)
      errors.add(:You, "can't specify the rate as a negative number.") unless '0'.to_d < rate.to_d
    end
  end
类顺序0,则费率和金额>0
存款金额=user.fund.amount,如果user.fund.amount.present?
金额=费率*金额
是否存在存款金额?
如果硬币到佐藤(金额)。到佐藤>硬币到佐藤(存款金额)。到佐藤
错误。添加(:您的“存入的日元短于订单金额”。)
结束
其他的
错误。添加(:您“尚未存入日元或您的交易尚未确认。”)
结束
其他的
错误。添加(:您“不能在此处指定负数”。)
结束
结束
结束
def至_参数
uuid
结束
def不足?
如果存在金额&&订单类型。拆分(“”)。最后一次=='sell'
if CurrencyUser.find_by(用户id:user.id,货币id:Pair.find_by(名称:Pair.currency_id)。金额<金额
错误。添加(:此订单的“金额”超过您的持有量。“)

错误[:base]您的create方法太复杂了,我假设您没有使用测试驱动开发编写此方法,因为如果您使用了,您会将其分解为更小的方法,可能会将更多的逻辑转移到模型中。我也希望这不是你的代码风格在现实生活中的样子。乍一看,我认为你的问题是:

 def create
   # why are you creating 2 orders??  Can you change this:
   @sell=current_user.orders.build(sell_params)
   @buy=current_user.orders.build(buy_params)

   #to something more like:

   @order = order_from_params
   #...omitting rest of this code because it needs a lot of refactoring.
 end

private 

def order_from_params
  #you should probably only deal with 1 order at a time either buy or sell
  # you must have a field in orders table to indicate buy or sell, probably int?
  if sell_params.present?
    @order = current_user.orders.build(sell_params)
  else
    @order = current_user.orders.build(buy_params)
  end
end

然后您需要简化create方法中的逻辑,因为您只有一个模型要处理。如果@order.save
在创建操作中,您不应依赖或执行2
if@order.save,因为您一次只能创建1个订单。这会让你朝着正确的方向前进。如果您不进行测试驱动开发,您将遇到比现有问题更糟糕的问题。

请在编辑器中使用rubocp和自动缩进。为了更好的可读性,您的代码已经用这两项重新格式化。首先,我必须检查订单是“买”还是“卖”,但有一种担心是表单中的隐藏值会被伪造,所以我创建了两种类型的实例,我知道我应该将逻辑放入模型中,但现在我正在快速地制作服务的核心,因此没有能力进行重构,如果我进行测试,我会回答,我刚才一直在测试订单是否都是和股票交易一样处理的,但在我开始改变功能后,我从未做过测试
 def create
   # why are you creating 2 orders??  Can you change this:
   @sell=current_user.orders.build(sell_params)
   @buy=current_user.orders.build(buy_params)

   #to something more like:

   @order = order_from_params
   #...omitting rest of this code because it needs a lot of refactoring.
 end

private 

def order_from_params
  #you should probably only deal with 1 order at a time either buy or sell
  # you must have a field in orders table to indicate buy or sell, probably int?
  if sell_params.present?
    @order = current_user.orders.build(sell_params)
  else
    @order = current_user.orders.build(buy_params)
  end
end