Rails4 Deprecation warning for find_or_initialize_by method










20















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 ?










share|improve this question




























    20















    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 ?










    share|improve this question


























      20












      20








      20


      3






      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 ?










      share|improve this question
















      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 ?







      ruby-on-rails ruby-on-rails-4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '14 at 18:14









      Simone Carletti

      145k34310333




      145k34310333










      asked Mar 18 '14 at 7:12









      Amrit DhunganaAmrit Dhungana

      2,54352333




      2,54352333






















          2 Answers
          2






          active

          oldest

          votes


















          28














          You can use the following:



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





          share|improve this answer
































            14














            # find_or_initialize_by_
            # Rails 3:
            Team.find_or_initialize_by_name('Justice League')
            # Rails 4:
            Team.find_or_initialize_by(name: 'Justice League')
            # or
            Team.where(name: 'Justice League').first_or_initialize


            NB: In these cases, the word 'initialize' will be like calling Team.new. You can also replace 'initialize' 'create' to instead call Team.create, like this: .find_or_create_by or .first_or_create



            Expanded and improved, but based on: http://blog.remarkablelabs.com/2012/12/what-s-new-in-active-record-rails-4-countdown-to-2013






            share|improve this answer




















            • 1





              For rails 4, itrs not working

              – Dinesh Saini
              Nov 17 '16 at 8:18










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22472584%2frails4-deprecation-warning-for-find-or-initialize-by-method%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            28














            You can use the following:



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





            share|improve this answer





























              28














              You can use the following:



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





              share|improve this answer



























                28












                28








                28







                You can use the following:



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





                share|improve this answer















                You can use the following:



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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 28 '15 at 7:21

























                answered Mar 18 '14 at 7:21









                rails4guides.comrails4guides.com

                1,311288




                1,311288























                    14














                    # find_or_initialize_by_
                    # Rails 3:
                    Team.find_or_initialize_by_name('Justice League')
                    # Rails 4:
                    Team.find_or_initialize_by(name: 'Justice League')
                    # or
                    Team.where(name: 'Justice League').first_or_initialize


                    NB: In these cases, the word 'initialize' will be like calling Team.new. You can also replace 'initialize' 'create' to instead call Team.create, like this: .find_or_create_by or .first_or_create



                    Expanded and improved, but based on: http://blog.remarkablelabs.com/2012/12/what-s-new-in-active-record-rails-4-countdown-to-2013






                    share|improve this answer




















                    • 1





                      For rails 4, itrs not working

                      – Dinesh Saini
                      Nov 17 '16 at 8:18















                    14














                    # find_or_initialize_by_
                    # Rails 3:
                    Team.find_or_initialize_by_name('Justice League')
                    # Rails 4:
                    Team.find_or_initialize_by(name: 'Justice League')
                    # or
                    Team.where(name: 'Justice League').first_or_initialize


                    NB: In these cases, the word 'initialize' will be like calling Team.new. You can also replace 'initialize' 'create' to instead call Team.create, like this: .find_or_create_by or .first_or_create



                    Expanded and improved, but based on: http://blog.remarkablelabs.com/2012/12/what-s-new-in-active-record-rails-4-countdown-to-2013






                    share|improve this answer




















                    • 1





                      For rails 4, itrs not working

                      – Dinesh Saini
                      Nov 17 '16 at 8:18













                    14












                    14








                    14







                    # find_or_initialize_by_
                    # Rails 3:
                    Team.find_or_initialize_by_name('Justice League')
                    # Rails 4:
                    Team.find_or_initialize_by(name: 'Justice League')
                    # or
                    Team.where(name: 'Justice League').first_or_initialize


                    NB: In these cases, the word 'initialize' will be like calling Team.new. You can also replace 'initialize' 'create' to instead call Team.create, like this: .find_or_create_by or .first_or_create



                    Expanded and improved, but based on: http://blog.remarkablelabs.com/2012/12/what-s-new-in-active-record-rails-4-countdown-to-2013






                    share|improve this answer















                    # find_or_initialize_by_
                    # Rails 3:
                    Team.find_or_initialize_by_name('Justice League')
                    # Rails 4:
                    Team.find_or_initialize_by(name: 'Justice League')
                    # or
                    Team.where(name: 'Justice League').first_or_initialize


                    NB: In these cases, the word 'initialize' will be like calling Team.new. You can also replace 'initialize' 'create' to instead call Team.create, like this: .find_or_create_by or .first_or_create



                    Expanded and improved, but based on: http://blog.remarkablelabs.com/2012/12/what-s-new-in-active-record-rails-4-countdown-to-2013







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 13 '18 at 13:14

























                    answered Jun 30 '15 at 11:51









                    MagneMagne

                    10.7k64559




                    10.7k64559







                    • 1





                      For rails 4, itrs not working

                      – Dinesh Saini
                      Nov 17 '16 at 8:18












                    • 1





                      For rails 4, itrs not working

                      – Dinesh Saini
                      Nov 17 '16 at 8:18







                    1




                    1





                    For rails 4, itrs not working

                    – Dinesh Saini
                    Nov 17 '16 at 8:18





                    For rails 4, itrs not working

                    – Dinesh Saini
                    Nov 17 '16 at 8:18

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22472584%2frails4-deprecation-warning-for-find-or-initialize-by-method%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党