Ruby on rails Rails字段更改值
我有3个模型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
货币
,神经网络
,预测
。每种货币都有一个与之相关的神经网络来创建预测
每次调用货币的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