ActiveModel dirty to ignore milliseconds











up vote
1
down vote

favorite












How can I make ActiveModel::Dirty ignore milliseconds when comparing datetime in Rails.



     news.publish_at
=> Fri, 16 Nov 2018 17:05:37 CET +01:00

news.publish_at = news.publish_at.to_s
=> "2018-11-16 17:05:37 +0100"

news.publish_at_changed?
=> true


But if I add milliseconds to the above datetime, changed? is false.



     news.publish_at = "2018-11-16 17:05:37.517 +0100"
=> "2018-11-16 17:05:37.517 +0100"

news.publish_at_changed?
=> false


I am using Rails 5.2.1










share|improve this question


















  • 1




    Did you consider removing the milliseconds automatically when assigning a datetime to publish_at?
    – spickermann
    Nov 17 at 15:24










  • @spickermann I tried that and seems to work fine. Could you add that as the answer so that I can accept it?
    – yogi_bear
    yesterday















up vote
1
down vote

favorite












How can I make ActiveModel::Dirty ignore milliseconds when comparing datetime in Rails.



     news.publish_at
=> Fri, 16 Nov 2018 17:05:37 CET +01:00

news.publish_at = news.publish_at.to_s
=> "2018-11-16 17:05:37 +0100"

news.publish_at_changed?
=> true


But if I add milliseconds to the above datetime, changed? is false.



     news.publish_at = "2018-11-16 17:05:37.517 +0100"
=> "2018-11-16 17:05:37.517 +0100"

news.publish_at_changed?
=> false


I am using Rails 5.2.1










share|improve this question


















  • 1




    Did you consider removing the milliseconds automatically when assigning a datetime to publish_at?
    – spickermann
    Nov 17 at 15:24










  • @spickermann I tried that and seems to work fine. Could you add that as the answer so that I can accept it?
    – yogi_bear
    yesterday













up vote
1
down vote

favorite









up vote
1
down vote

favorite











How can I make ActiveModel::Dirty ignore milliseconds when comparing datetime in Rails.



     news.publish_at
=> Fri, 16 Nov 2018 17:05:37 CET +01:00

news.publish_at = news.publish_at.to_s
=> "2018-11-16 17:05:37 +0100"

news.publish_at_changed?
=> true


But if I add milliseconds to the above datetime, changed? is false.



     news.publish_at = "2018-11-16 17:05:37.517 +0100"
=> "2018-11-16 17:05:37.517 +0100"

news.publish_at_changed?
=> false


I am using Rails 5.2.1










share|improve this question













How can I make ActiveModel::Dirty ignore milliseconds when comparing datetime in Rails.



     news.publish_at
=> Fri, 16 Nov 2018 17:05:37 CET +01:00

news.publish_at = news.publish_at.to_s
=> "2018-11-16 17:05:37 +0100"

news.publish_at_changed?
=> true


But if I add milliseconds to the above datetime, changed? is false.



     news.publish_at = "2018-11-16 17:05:37.517 +0100"
=> "2018-11-16 17:05:37.517 +0100"

news.publish_at_changed?
=> false


I am using Rails 5.2.1







ruby-on-rails ruby ruby-on-rails-5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 at 14:30









yogi_bear

8818




8818








  • 1




    Did you consider removing the milliseconds automatically when assigning a datetime to publish_at?
    – spickermann
    Nov 17 at 15:24










  • @spickermann I tried that and seems to work fine. Could you add that as the answer so that I can accept it?
    – yogi_bear
    yesterday














  • 1




    Did you consider removing the milliseconds automatically when assigning a datetime to publish_at?
    – spickermann
    Nov 17 at 15:24










  • @spickermann I tried that and seems to work fine. Could you add that as the answer so that I can accept it?
    – yogi_bear
    yesterday








1




1




Did you consider removing the milliseconds automatically when assigning a datetime to publish_at?
– spickermann
Nov 17 at 15:24




Did you consider removing the milliseconds automatically when assigning a datetime to publish_at?
– spickermann
Nov 17 at 15:24












@spickermann I tried that and seems to work fine. Could you add that as the answer so that I can accept it?
– yogi_bear
yesterday




@spickermann I tried that and seems to work fine. Could you add that as the answer so that I can accept it?
– yogi_bear
yesterday












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I think the simplest workaround might be to just add a custom setter to that model that truncates the milliseconds everytime when assigning a new value.



def publish_at=(value)
if value
time_without_usec = DateTime.parse(value).change(usec: 0)
value = time_without_usec.to_s
end

super(value)
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',
    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%2f53352168%2factivemodel-dirty-to-ignore-milliseconds%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    I think the simplest workaround might be to just add a custom setter to that model that truncates the milliseconds everytime when assigning a new value.



    def publish_at=(value)
    if value
    time_without_usec = DateTime.parse(value).change(usec: 0)
    value = time_without_usec.to_s
    end

    super(value)
    end





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      I think the simplest workaround might be to just add a custom setter to that model that truncates the milliseconds everytime when assigning a new value.



      def publish_at=(value)
      if value
      time_without_usec = DateTime.parse(value).change(usec: 0)
      value = time_without_usec.to_s
      end

      super(value)
      end





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        I think the simplest workaround might be to just add a custom setter to that model that truncates the milliseconds everytime when assigning a new value.



        def publish_at=(value)
        if value
        time_without_usec = DateTime.parse(value).change(usec: 0)
        value = time_without_usec.to_s
        end

        super(value)
        end





        share|improve this answer












        I think the simplest workaround might be to just add a custom setter to that model that truncates the milliseconds everytime when assigning a new value.



        def publish_at=(value)
        if value
        time_without_usec = DateTime.parse(value).change(usec: 0)
        value = time_without_usec.to_s
        end

        super(value)
        end






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        spickermann

        57.7k65375




        57.7k65375






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352168%2factivemodel-dirty-to-ignore-milliseconds%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin