Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails4 Deprecation warning for find_or_initialize_by method

Tags:

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 ?

like image 804
Amrit Dhungana Avatar asked Mar 18 '14 07:03

Amrit Dhungana


1 Answers

You can use the following:

Progress.find_or_initialize_by(chore_id: chore.id, period: period[chore.frequency], account_id: chore.account_id) 
like image 162
rails4guides.com Avatar answered Oct 21 '22 05:10

rails4guides.com