Can I timestamp a specific field in a rails db?










0















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


























    0















    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
























      0












      0








      0








      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 '18 at 20:05









      willwill

      31




      31






















          2 Answers
          2






          active

          oldest

          votes


















          1














          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






























            1














            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',
              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%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









              1














              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



























                1














                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

























                  1












                  1








                  1







                  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 '18 at 20:23









                  Carl MarkhamCarl Markham

                  6,17623063




                  6,17623063























                      1














                      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





























                        1














                        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



























                          1












                          1








                          1







                          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 '18 at 13:41

























                          answered Nov 15 '18 at 21:22









                          showaltbshowaltb

                          818410




                          818410



























                              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%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

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              ReactJS Fetched API data displays live - need Data displayed static

                              政党