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
ruby-on-rails ruby ruby-on-rails-5
add a comment |
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
ruby-on-rails ruby ruby-on-rails-5
1
Did you consider removing the milliseconds automatically when assigning a datetime topublish_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
add a comment |
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
ruby-on-rails ruby ruby-on-rails-5
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
ruby-on-rails ruby ruby-on-rails-5
asked Nov 17 at 14:30
yogi_bear
8818
8818
1
Did you consider removing the milliseconds automatically when assigning a datetime topublish_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
add a comment |
1
Did you consider removing the milliseconds automatically when assigning a datetime topublish_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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered yesterday
spickermann
57.7k65375
57.7k65375
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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