gpt4 book ai didi

ruby-on-rails - find_or_initialize_by方法的Rails弃用警告

转载 作者:行者123 更新时间:2023-12-03 12:06:14 25 4
gpt4 key购买 nike

我正在从3.2升级到Rails4。我有以下查询:

progress = Progress.find_or_initialize_by_chore_id_and_period_and_account_id(chore.id, period[chore.frequency], chore.account_id)

运行测试时,我收到了弃用警告
DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_initialize_by(name: 'foo') instead. (called from bump_progress at /Users/newimac/RailsApp/bank/app/models/child.rb:200)

因此,我将查询更新如下:
progress = Progress.where('chore.id' => 'chore_id', 'period[chore.frequency]' => 'period', 'chore.account_id' => 'account_id').first_or_initialize

但是它不起作用。我的查询正确吗?

最佳答案

您可以使用以下内容:

Progress.find_or_initialize_by(chore_id: chore.id, period: period[chore.frequency], account_id: chore.account_id) 

关于ruby-on-rails - find_or_initialize_by方法的Rails弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22472584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com