find_or_initialize_by 方法的 Rails4 弃用警告 [英] Rails4 Deprecation warning for find_or_initialize_by method

查看:39
本文介绍了find_or_initialize_by 方法的 Rails4 弃用警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am upgrading to Rails4 from 3.2. I have the following query:

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

在运行测试时,我收到弃用警告

While running test, I am getting deprecation warning

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)

所以,我更新了我的查询如下:

So, I have updated my query as follows:

progress = Progress.where('chore.id' => 'chore_id', 'period[chore.frequency]' => 'period', 'chore.account_id' => 'account_id').first_or_initialize

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

But its not working. Is my query correct ?

推荐答案

您可以使用以下内容:

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

这篇关于find_or_initialize_by 方法的 Rails4 弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆