Can I timestamp a specific field in a rails db?











up vote
0
down vote

favorite












I have a form with a checkbox that users tick to provide consent. When this form is posted the Users table is updated with a field that confirms consent is provided. However I also want record the date/time that consent was provided. To this end I was hoping to have a 'user_consent' field that would store 'yes' and a user_consent_agreed' field that would store the date/time that the user_consent field was updated.



I know I can add timestamps to a table on creation but this will change when any attribute is updated, I need to record time/date in its own field when the consent attribute changes.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a form with a checkbox that users tick to provide consent. When this form is posted the Users table is updated with a field that confirms consent is provided. However I also want record the date/time that consent was provided. To this end I was hoping to have a 'user_consent' field that would store 'yes' and a user_consent_agreed' field that would store the date/time that the user_consent field was updated.



    I know I can add timestamps to a table on creation but this will change when any attribute is updated, I need to record time/date in its own field when the consent attribute changes.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a form with a checkbox that users tick to provide consent. When this form is posted the Users table is updated with a field that confirms consent is provided. However I also want record the date/time that consent was provided. To this end I was hoping to have a 'user_consent' field that would store 'yes' and a user_consent_agreed' field that would store the date/time that the user_consent field was updated.



      I know I can add timestamps to a table on creation but this will change when any attribute is updated, I need to record time/date in its own field when the consent attribute changes.










      share|improve this question













      I have a form with a checkbox that users tick to provide consent. When this form is posted the Users table is updated with a field that confirms consent is provided. However I also want record the date/time that consent was provided. To this end I was hoping to have a 'user_consent' field that would store 'yes' and a user_consent_agreed' field that would store the date/time that the user_consent field was updated.



      I know I can add timestamps to a table on creation but this will change when any attribute is updated, I need to record time/date in its own field when the consent attribute changes.







      ruby-on-rails






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 at 20:05









      will

      51




      51
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          I would just have one column in the table consented_at, which is of type DateTime. When they consent, you update that column:



          user.update_column :consented_at, DateTime.now



          I used consented_at to keep it inline with created_at and updated_at



          This way, you can tell if a person has consented or not and when they consented (if they did).



          if user.consented_at?
          # consented and you have the time in consented_at
          else
          # never consented
          end


          No real need to have a boolean column and a time since checking the time column will return true or false anyway.






          share|improve this answer




























            up vote
            1
            down vote













            As already suggested, add a consented_at column to hold the timestamp.



            To automatically update this column when the value of consented changes, add a before_save callback to your model:



            class User < ApplicationRecord

            before_save do
            self.consented_at = Time.zone.now if consented_changed?
            end

            end


            Note that this will set consented_at on any change to consented, even setting it to false. If that's not what you want, you can adjust the code inside the callback as desired.



            See https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html for more on model callbacks. See https://api.rubyonrails.org/classes/ActiveModel/Dirty.html for more on how to detect changes.






            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',
              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%2f53327148%2fcan-i-timestamp-a-specific-field-in-a-rails-db%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








              up vote
              1
              down vote













              I would just have one column in the table consented_at, which is of type DateTime. When they consent, you update that column:



              user.update_column :consented_at, DateTime.now



              I used consented_at to keep it inline with created_at and updated_at



              This way, you can tell if a person has consented or not and when they consented (if they did).



              if user.consented_at?
              # consented and you have the time in consented_at
              else
              # never consented
              end


              No real need to have a boolean column and a time since checking the time column will return true or false anyway.






              share|improve this answer

























                up vote
                1
                down vote













                I would just have one column in the table consented_at, which is of type DateTime. When they consent, you update that column:



                user.update_column :consented_at, DateTime.now



                I used consented_at to keep it inline with created_at and updated_at



                This way, you can tell if a person has consented or not and when they consented (if they did).



                if user.consented_at?
                # consented and you have the time in consented_at
                else
                # never consented
                end


                No real need to have a boolean column and a time since checking the time column will return true or false anyway.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  I would just have one column in the table consented_at, which is of type DateTime. When they consent, you update that column:



                  user.update_column :consented_at, DateTime.now



                  I used consented_at to keep it inline with created_at and updated_at



                  This way, you can tell if a person has consented or not and when they consented (if they did).



                  if user.consented_at?
                  # consented and you have the time in consented_at
                  else
                  # never consented
                  end


                  No real need to have a boolean column and a time since checking the time column will return true or false anyway.






                  share|improve this answer












                  I would just have one column in the table consented_at, which is of type DateTime. When they consent, you update that column:



                  user.update_column :consented_at, DateTime.now



                  I used consented_at to keep it inline with created_at and updated_at



                  This way, you can tell if a person has consented or not and when they consented (if they did).



                  if user.consented_at?
                  # consented and you have the time in consented_at
                  else
                  # never consented
                  end


                  No real need to have a boolean column and a time since checking the time column will return true or false anyway.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 at 20:23









                  Carl Markham

                  5,98222761




                  5,98222761
























                      up vote
                      1
                      down vote













                      As already suggested, add a consented_at column to hold the timestamp.



                      To automatically update this column when the value of consented changes, add a before_save callback to your model:



                      class User < ApplicationRecord

                      before_save do
                      self.consented_at = Time.zone.now if consented_changed?
                      end

                      end


                      Note that this will set consented_at on any change to consented, even setting it to false. If that's not what you want, you can adjust the code inside the callback as desired.



                      See https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html for more on model callbacks. See https://api.rubyonrails.org/classes/ActiveModel/Dirty.html for more on how to detect changes.






                      share|improve this answer



























                        up vote
                        1
                        down vote













                        As already suggested, add a consented_at column to hold the timestamp.



                        To automatically update this column when the value of consented changes, add a before_save callback to your model:



                        class User < ApplicationRecord

                        before_save do
                        self.consented_at = Time.zone.now if consented_changed?
                        end

                        end


                        Note that this will set consented_at on any change to consented, even setting it to false. If that's not what you want, you can adjust the code inside the callback as desired.



                        See https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html for more on model callbacks. See https://api.rubyonrails.org/classes/ActiveModel/Dirty.html for more on how to detect changes.






                        share|improve this answer

























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          As already suggested, add a consented_at column to hold the timestamp.



                          To automatically update this column when the value of consented changes, add a before_save callback to your model:



                          class User < ApplicationRecord

                          before_save do
                          self.consented_at = Time.zone.now if consented_changed?
                          end

                          end


                          Note that this will set consented_at on any change to consented, even setting it to false. If that's not what you want, you can adjust the code inside the callback as desired.



                          See https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html for more on model callbacks. See https://api.rubyonrails.org/classes/ActiveModel/Dirty.html for more on how to detect changes.






                          share|improve this answer














                          As already suggested, add a consented_at column to hold the timestamp.



                          To automatically update this column when the value of consented changes, add a before_save callback to your model:



                          class User < ApplicationRecord

                          before_save do
                          self.consented_at = Time.zone.now if consented_changed?
                          end

                          end


                          Note that this will set consented_at on any change to consented, even setting it to false. If that's not what you want, you can adjust the code inside the callback as desired.



                          See https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html for more on model callbacks. See https://api.rubyonrails.org/classes/ActiveModel/Dirty.html for more on how to detect changes.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 16 at 13:41

























                          answered Nov 15 at 21:22









                          showaltb

                          77839




                          77839






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53327148%2fcan-i-timestamp-a-specific-field-in-a-rails-db%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

                              mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                              How to change which sound is reproduced for terminal bell?

                              Can I use Tabulator js library in my java Spring + Thymeleaf project?