Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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_Model View Controller - Fatal编程技术网

Ruby on rails Rails字段更改值

Ruby on rails Rails字段更改值,ruby-on-rails,model-view-controller,Ruby On Rails,Model View Controller,我有3个模型货币,神经网络,预测。每种货币都有一个与之相关的神经网络来创建预测 每次调用货币的update方法时,它都会要求神经网络生成新的预测或使用已生成的预测。货币有一个神经网络,它有一个预测 货币控制员: def update if @currency.update(currency_params) prediction = @currency.neural_network.predict redirect_to prediction end e

我有3个模型
货币
神经网络
预测
。每种货币都有一个与之相关的神经网络来创建预测

每次调用货币的
update
方法时,它都会要求神经网络生成新的预测或使用已生成的预测。货币
有一个
神经网络,它
有一个
预测

货币控制员:

def update
    if @currency.update(currency_params)
      prediction = @currency.neural_network.predict
      redirect_to prediction
    end
  end
神经网络模型

  def predict
    if(self.prediction == nil)
      #some other code
      (0..29).each do |i|
        #ASS: I'm getting the data for today at the beginning of the day
        self.prediction.exchange_rates << ExchangeRate.new(last: predicted_rates[i], date: Date.today + i + 1, predicted: true)
      end
    end
    p "there are #{self.prediction.exchange_rates.size} predicted rates"
    self.prediction
  end

为什么字段会更改其值?为什么循环中创建的对象没有被记住?

我认为这是因为您没有保存
预测
,所以它第二次也会进入循环,因为
self.prediction==nil
是条件再次为
,即使调用
prediction.save
,我仍然得到相同的输出。看看第二次它是否也进入循环,将某些东西放入循环,它不是,也不应该,因为预测已经构建好了。
    there are 30 predicted rates
    there are 0 predicted rates