How to apply `where` condition to each record












0















I want to display the difference_in_days for every HPId, which is present in a table AccountClose.



Here is my code



@a = AccountClose.where("AccountCloseId is not null").last.Date.to_date
@before = Date.today
@difference_in_days = (@before.to_date - @a.to_date).to_i


The above query only displays the difference_in_days for the last record. Could anyone help me with this?










share|improve this question

























  • You are getting difference_in_days only for the last record because you are fetching the last record alone. @a = AccountClose.where("AccountCloseId is not null").last.Date.to_date the last in this statement fetches just the last record. Hope it clarifies your doubt.

    – Surya
    Nov 21 '18 at 4:49






  • 1





    can you show AccountClose schema or table structure?

    – Gabbar
    Nov 21 '18 at 4:54











  • AccountClose(AccountCloseId: integer, HPId: integer, UsersId: integer, Date: datetime)

    – TChaitanya Tatavolu
    Nov 21 '18 at 5:00











  • @TChaitanyaTatavolu You can check the given answer below that's the difference between answer you have marked correct .

    – Gabbar
    Nov 21 '18 at 5:45
















0















I want to display the difference_in_days for every HPId, which is present in a table AccountClose.



Here is my code



@a = AccountClose.where("AccountCloseId is not null").last.Date.to_date
@before = Date.today
@difference_in_days = (@before.to_date - @a.to_date).to_i


The above query only displays the difference_in_days for the last record. Could anyone help me with this?










share|improve this question

























  • You are getting difference_in_days only for the last record because you are fetching the last record alone. @a = AccountClose.where("AccountCloseId is not null").last.Date.to_date the last in this statement fetches just the last record. Hope it clarifies your doubt.

    – Surya
    Nov 21 '18 at 4:49






  • 1





    can you show AccountClose schema or table structure?

    – Gabbar
    Nov 21 '18 at 4:54











  • AccountClose(AccountCloseId: integer, HPId: integer, UsersId: integer, Date: datetime)

    – TChaitanya Tatavolu
    Nov 21 '18 at 5:00











  • @TChaitanyaTatavolu You can check the given answer below that's the difference between answer you have marked correct .

    – Gabbar
    Nov 21 '18 at 5:45














0












0








0








I want to display the difference_in_days for every HPId, which is present in a table AccountClose.



Here is my code



@a = AccountClose.where("AccountCloseId is not null").last.Date.to_date
@before = Date.today
@difference_in_days = (@before.to_date - @a.to_date).to_i


The above query only displays the difference_in_days for the last record. Could anyone help me with this?










share|improve this question
















I want to display the difference_in_days for every HPId, which is present in a table AccountClose.



Here is my code



@a = AccountClose.where("AccountCloseId is not null").last.Date.to_date
@before = Date.today
@difference_in_days = (@before.to_date - @a.to_date).to_i


The above query only displays the difference_in_days for the last record. Could anyone help me with this?







ruby-on-rails ruby-on-rails-3.2 rails-migrations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 5:38









sawa

130k29202301




130k29202301










asked Nov 21 '18 at 4:38









TChaitanya TatavoluTChaitanya Tatavolu

64




64













  • You are getting difference_in_days only for the last record because you are fetching the last record alone. @a = AccountClose.where("AccountCloseId is not null").last.Date.to_date the last in this statement fetches just the last record. Hope it clarifies your doubt.

    – Surya
    Nov 21 '18 at 4:49






  • 1





    can you show AccountClose schema or table structure?

    – Gabbar
    Nov 21 '18 at 4:54











  • AccountClose(AccountCloseId: integer, HPId: integer, UsersId: integer, Date: datetime)

    – TChaitanya Tatavolu
    Nov 21 '18 at 5:00











  • @TChaitanyaTatavolu You can check the given answer below that's the difference between answer you have marked correct .

    – Gabbar
    Nov 21 '18 at 5:45



















  • You are getting difference_in_days only for the last record because you are fetching the last record alone. @a = AccountClose.where("AccountCloseId is not null").last.Date.to_date the last in this statement fetches just the last record. Hope it clarifies your doubt.

    – Surya
    Nov 21 '18 at 4:49






  • 1





    can you show AccountClose schema or table structure?

    – Gabbar
    Nov 21 '18 at 4:54











  • AccountClose(AccountCloseId: integer, HPId: integer, UsersId: integer, Date: datetime)

    – TChaitanya Tatavolu
    Nov 21 '18 at 5:00











  • @TChaitanyaTatavolu You can check the given answer below that's the difference between answer you have marked correct .

    – Gabbar
    Nov 21 '18 at 5:45

















You are getting difference_in_days only for the last record because you are fetching the last record alone. @a = AccountClose.where("AccountCloseId is not null").last.Date.to_date the last in this statement fetches just the last record. Hope it clarifies your doubt.

– Surya
Nov 21 '18 at 4:49





You are getting difference_in_days only for the last record because you are fetching the last record alone. @a = AccountClose.where("AccountCloseId is not null").last.Date.to_date the last in this statement fetches just the last record. Hope it clarifies your doubt.

– Surya
Nov 21 '18 at 4:49




1




1





can you show AccountClose schema or table structure?

– Gabbar
Nov 21 '18 at 4:54





can you show AccountClose schema or table structure?

– Gabbar
Nov 21 '18 at 4:54













AccountClose(AccountCloseId: integer, HPId: integer, UsersId: integer, Date: datetime)

– TChaitanya Tatavolu
Nov 21 '18 at 5:00





AccountClose(AccountCloseId: integer, HPId: integer, UsersId: integer, Date: datetime)

– TChaitanya Tatavolu
Nov 21 '18 at 5:00













@TChaitanyaTatavolu You can check the given answer below that's the difference between answer you have marked correct .

– Gabbar
Nov 21 '18 at 5:45





@TChaitanyaTatavolu You can check the given answer below that's the difference between answer you have marked correct .

– Gabbar
Nov 21 '18 at 5:45












3 Answers
3






active

oldest

votes


















0















a sidenote ...



you really should try to follow the rails way. convention over
configuration. that means to take care how to name you database,
tables and fields and lots more. it makes life much easier.



https://en.wikipedia.org/wiki/Ruby_on_Rails
https://en.wikibooks.org/wiki/Ruby_on_Rails







share|improve this answer































    1














    @before = Date.today 
    @a = AccountClose.where("AccountCloseId is not null")
    @a.each_with_index{|account_close,i|
    @difference_in_days = (@before.to_date - account_close.Date.to_date).to_i
    puts "Difference in days for account close #{i}: t #{@difference_in_days}"
    }





    share|improve this answer


























    • Thank you!!!But it's showing me the error like 'undefined method to_date'

      – TChaitanya Tatavolu
      Nov 21 '18 at 4:51













    • what is the error?

      – Surya
      Nov 21 '18 at 4:52











    • NoMethodError (undefined method `to_date' for #<ActiveRecord::Relation:0x0000002ddd6760>

      – TChaitanya Tatavolu
      Nov 21 '18 at 4:53











    • what are the date columns in AccountClose? a.to_date in this line you need to add a.date_column.to_date

      – Surya
      Nov 21 '18 at 4:55






    • 1





      @TChaitanyaTatavolu How did you accept the answer if its still showing error? lol

      – Gabbar
      Nov 21 '18 at 4:56



















    0














    Fetch all closed_dates using pluck query as array



    @closed_dates = AccountClose.where("AccountCloseId is not null").pluck('date(Date)')


    => [Fri, 20 Jan 2017, Sun, 22 Jan 2017, Sun, 22 Jan 2017, Thu, 02 Feb 2017, Tue, 26 Jun 2018, Wed, 27 Jun 2018, Mon, 02 Jul 2018, Wed, 04 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Thu, 12 Jul 2018, Thu, 12 Jul 2018, Fri, 13 Jul 2018..]



    Display difference_in_days for every HPId which is present in AccountClose



    @closed_dates.each do |colsed_date|
    puts "Difference in days is #{(Date.today - colsed_date).to_i}"
    end





    share|improve this answer























      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%2f53405348%2fhow-to-apply-where-condition-to-each-record%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0















      a sidenote ...



      you really should try to follow the rails way. convention over
      configuration. that means to take care how to name you database,
      tables and fields and lots more. it makes life much easier.



      https://en.wikipedia.org/wiki/Ruby_on_Rails
      https://en.wikibooks.org/wiki/Ruby_on_Rails







      share|improve this answer




























        0















        a sidenote ...



        you really should try to follow the rails way. convention over
        configuration. that means to take care how to name you database,
        tables and fields and lots more. it makes life much easier.



        https://en.wikipedia.org/wiki/Ruby_on_Rails
        https://en.wikibooks.org/wiki/Ruby_on_Rails







        share|improve this answer


























          0












          0








          0








          a sidenote ...



          you really should try to follow the rails way. convention over
          configuration. that means to take care how to name you database,
          tables and fields and lots more. it makes life much easier.



          https://en.wikipedia.org/wiki/Ruby_on_Rails
          https://en.wikibooks.org/wiki/Ruby_on_Rails







          share|improve this answer














          a sidenote ...



          you really should try to follow the rails way. convention over
          configuration. that means to take care how to name you database,
          tables and fields and lots more. it makes life much easier.



          https://en.wikipedia.org/wiki/Ruby_on_Rails
          https://en.wikibooks.org/wiki/Ruby_on_Rails








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 10:26









          devananddevanand

          3,3571318




          3,3571318

























              1














              @before = Date.today 
              @a = AccountClose.where("AccountCloseId is not null")
              @a.each_with_index{|account_close,i|
              @difference_in_days = (@before.to_date - account_close.Date.to_date).to_i
              puts "Difference in days for account close #{i}: t #{@difference_in_days}"
              }





              share|improve this answer


























              • Thank you!!!But it's showing me the error like 'undefined method to_date'

                – TChaitanya Tatavolu
                Nov 21 '18 at 4:51













              • what is the error?

                – Surya
                Nov 21 '18 at 4:52











              • NoMethodError (undefined method `to_date' for #<ActiveRecord::Relation:0x0000002ddd6760>

                – TChaitanya Tatavolu
                Nov 21 '18 at 4:53











              • what are the date columns in AccountClose? a.to_date in this line you need to add a.date_column.to_date

                – Surya
                Nov 21 '18 at 4:55






              • 1





                @TChaitanyaTatavolu How did you accept the answer if its still showing error? lol

                – Gabbar
                Nov 21 '18 at 4:56
















              1














              @before = Date.today 
              @a = AccountClose.where("AccountCloseId is not null")
              @a.each_with_index{|account_close,i|
              @difference_in_days = (@before.to_date - account_close.Date.to_date).to_i
              puts "Difference in days for account close #{i}: t #{@difference_in_days}"
              }





              share|improve this answer


























              • Thank you!!!But it's showing me the error like 'undefined method to_date'

                – TChaitanya Tatavolu
                Nov 21 '18 at 4:51













              • what is the error?

                – Surya
                Nov 21 '18 at 4:52











              • NoMethodError (undefined method `to_date' for #<ActiveRecord::Relation:0x0000002ddd6760>

                – TChaitanya Tatavolu
                Nov 21 '18 at 4:53











              • what are the date columns in AccountClose? a.to_date in this line you need to add a.date_column.to_date

                – Surya
                Nov 21 '18 at 4:55






              • 1





                @TChaitanyaTatavolu How did you accept the answer if its still showing error? lol

                – Gabbar
                Nov 21 '18 at 4:56














              1












              1








              1







              @before = Date.today 
              @a = AccountClose.where("AccountCloseId is not null")
              @a.each_with_index{|account_close,i|
              @difference_in_days = (@before.to_date - account_close.Date.to_date).to_i
              puts "Difference in days for account close #{i}: t #{@difference_in_days}"
              }





              share|improve this answer















              @before = Date.today 
              @a = AccountClose.where("AccountCloseId is not null")
              @a.each_with_index{|account_close,i|
              @difference_in_days = (@before.to_date - account_close.Date.to_date).to_i
              puts "Difference in days for account close #{i}: t #{@difference_in_days}"
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 21 '18 at 4:59

























              answered Nov 21 '18 at 4:48









              SuryaSurya

              327111




              327111













              • Thank you!!!But it's showing me the error like 'undefined method to_date'

                – TChaitanya Tatavolu
                Nov 21 '18 at 4:51













              • what is the error?

                – Surya
                Nov 21 '18 at 4:52











              • NoMethodError (undefined method `to_date' for #<ActiveRecord::Relation:0x0000002ddd6760>

                – TChaitanya Tatavolu
                Nov 21 '18 at 4:53











              • what are the date columns in AccountClose? a.to_date in this line you need to add a.date_column.to_date

                – Surya
                Nov 21 '18 at 4:55






              • 1





                @TChaitanyaTatavolu How did you accept the answer if its still showing error? lol

                – Gabbar
                Nov 21 '18 at 4:56



















              • Thank you!!!But it's showing me the error like 'undefined method to_date'

                – TChaitanya Tatavolu
                Nov 21 '18 at 4:51













              • what is the error?

                – Surya
                Nov 21 '18 at 4:52











              • NoMethodError (undefined method `to_date' for #<ActiveRecord::Relation:0x0000002ddd6760>

                – TChaitanya Tatavolu
                Nov 21 '18 at 4:53











              • what are the date columns in AccountClose? a.to_date in this line you need to add a.date_column.to_date

                – Surya
                Nov 21 '18 at 4:55






              • 1





                @TChaitanyaTatavolu How did you accept the answer if its still showing error? lol

                – Gabbar
                Nov 21 '18 at 4:56

















              Thank you!!!But it's showing me the error like 'undefined method to_date'

              – TChaitanya Tatavolu
              Nov 21 '18 at 4:51







              Thank you!!!But it's showing me the error like 'undefined method to_date'

              – TChaitanya Tatavolu
              Nov 21 '18 at 4:51















              what is the error?

              – Surya
              Nov 21 '18 at 4:52





              what is the error?

              – Surya
              Nov 21 '18 at 4:52













              NoMethodError (undefined method `to_date' for #<ActiveRecord::Relation:0x0000002ddd6760>

              – TChaitanya Tatavolu
              Nov 21 '18 at 4:53





              NoMethodError (undefined method `to_date' for #<ActiveRecord::Relation:0x0000002ddd6760>

              – TChaitanya Tatavolu
              Nov 21 '18 at 4:53













              what are the date columns in AccountClose? a.to_date in this line you need to add a.date_column.to_date

              – Surya
              Nov 21 '18 at 4:55





              what are the date columns in AccountClose? a.to_date in this line you need to add a.date_column.to_date

              – Surya
              Nov 21 '18 at 4:55




              1




              1





              @TChaitanyaTatavolu How did you accept the answer if its still showing error? lol

              – Gabbar
              Nov 21 '18 at 4:56





              @TChaitanyaTatavolu How did you accept the answer if its still showing error? lol

              – Gabbar
              Nov 21 '18 at 4:56











              0














              Fetch all closed_dates using pluck query as array



              @closed_dates = AccountClose.where("AccountCloseId is not null").pluck('date(Date)')


              => [Fri, 20 Jan 2017, Sun, 22 Jan 2017, Sun, 22 Jan 2017, Thu, 02 Feb 2017, Tue, 26 Jun 2018, Wed, 27 Jun 2018, Mon, 02 Jul 2018, Wed, 04 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Thu, 12 Jul 2018, Thu, 12 Jul 2018, Fri, 13 Jul 2018..]



              Display difference_in_days for every HPId which is present in AccountClose



              @closed_dates.each do |colsed_date|
              puts "Difference in days is #{(Date.today - colsed_date).to_i}"
              end





              share|improve this answer




























                0














                Fetch all closed_dates using pluck query as array



                @closed_dates = AccountClose.where("AccountCloseId is not null").pluck('date(Date)')


                => [Fri, 20 Jan 2017, Sun, 22 Jan 2017, Sun, 22 Jan 2017, Thu, 02 Feb 2017, Tue, 26 Jun 2018, Wed, 27 Jun 2018, Mon, 02 Jul 2018, Wed, 04 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Thu, 12 Jul 2018, Thu, 12 Jul 2018, Fri, 13 Jul 2018..]



                Display difference_in_days for every HPId which is present in AccountClose



                @closed_dates.each do |colsed_date|
                puts "Difference in days is #{(Date.today - colsed_date).to_i}"
                end





                share|improve this answer


























                  0












                  0








                  0







                  Fetch all closed_dates using pluck query as array



                  @closed_dates = AccountClose.where("AccountCloseId is not null").pluck('date(Date)')


                  => [Fri, 20 Jan 2017, Sun, 22 Jan 2017, Sun, 22 Jan 2017, Thu, 02 Feb 2017, Tue, 26 Jun 2018, Wed, 27 Jun 2018, Mon, 02 Jul 2018, Wed, 04 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Thu, 12 Jul 2018, Thu, 12 Jul 2018, Fri, 13 Jul 2018..]



                  Display difference_in_days for every HPId which is present in AccountClose



                  @closed_dates.each do |colsed_date|
                  puts "Difference in days is #{(Date.today - colsed_date).to_i}"
                  end





                  share|improve this answer













                  Fetch all closed_dates using pluck query as array



                  @closed_dates = AccountClose.where("AccountCloseId is not null").pluck('date(Date)')


                  => [Fri, 20 Jan 2017, Sun, 22 Jan 2017, Sun, 22 Jan 2017, Thu, 02 Feb 2017, Tue, 26 Jun 2018, Wed, 27 Jun 2018, Mon, 02 Jul 2018, Wed, 04 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Wed, 11 Jul 2018, Thu, 12 Jul 2018, Thu, 12 Jul 2018, Fri, 13 Jul 2018..]



                  Display difference_in_days for every HPId which is present in AccountClose



                  @closed_dates.each do |colsed_date|
                  puts "Difference in days is #{(Date.today - colsed_date).to_i}"
                  end






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 5:43









                  GabbarGabbar

                  4,0012417




                  4,0012417






























                      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%2f53405348%2fhow-to-apply-where-condition-to-each-record%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

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?