How to deal with data that expires every day?
I'm creating a rails app and I have a question.
I have data stored in a table that represents user limits on the system: how many times user answer a question (10 per day) for example.
id - count - user_id
1 3 1
--------------------
2 5 2
--------------------
3 10 3
So I need to reset the count column every day at midnight.
Is there a way I do not need to user a worker to run every day ?
ruby-on-rails worker
add a comment |
I'm creating a rails app and I have a question.
I have data stored in a table that represents user limits on the system: how many times user answer a question (10 per day) for example.
id - count - user_id
1 3 1
--------------------
2 5 2
--------------------
3 10 3
So I need to reset the count column every day at midnight.
Is there a way I do not need to user a worker to run every day ?
ruby-on-rails worker
no. I don't need a simple validation. I need to update the column with the counter and reset every nigh. thanks.
– An j
Nov 25 '18 at 17:52
you can use a background job, which will execute every day and update the column on specific time duration
– rohit
Nov 26 '18 at 6:28
add a comment |
I'm creating a rails app and I have a question.
I have data stored in a table that represents user limits on the system: how many times user answer a question (10 per day) for example.
id - count - user_id
1 3 1
--------------------
2 5 2
--------------------
3 10 3
So I need to reset the count column every day at midnight.
Is there a way I do not need to user a worker to run every day ?
ruby-on-rails worker
I'm creating a rails app and I have a question.
I have data stored in a table that represents user limits on the system: how many times user answer a question (10 per day) for example.
id - count - user_id
1 3 1
--------------------
2 5 2
--------------------
3 10 3
So I need to reset the count column every day at midnight.
Is there a way I do not need to user a worker to run every day ?
ruby-on-rails worker
ruby-on-rails worker
asked Nov 25 '18 at 0:58
An jAn j
1
1
no. I don't need a simple validation. I need to update the column with the counter and reset every nigh. thanks.
– An j
Nov 25 '18 at 17:52
you can use a background job, which will execute every day and update the column on specific time duration
– rohit
Nov 26 '18 at 6:28
add a comment |
no. I don't need a simple validation. I need to update the column with the counter and reset every nigh. thanks.
– An j
Nov 25 '18 at 17:52
you can use a background job, which will execute every day and update the column on specific time duration
– rohit
Nov 26 '18 at 6:28
no. I don't need a simple validation. I need to update the column with the counter and reset every nigh. thanks.
– An j
Nov 25 '18 at 17:52
no. I don't need a simple validation. I need to update the column with the counter and reset every nigh. thanks.
– An j
Nov 25 '18 at 17:52
you can use a background job, which will execute every day and update the column on specific time duration
– rohit
Nov 26 '18 at 6:28
you can use a background job, which will execute every day and update the column on specific time duration
– rohit
Nov 26 '18 at 6:28
add a comment |
2 Answers
2
active
oldest
votes
There are many, many solutions to this problem, so you'd have to decide what is acceptable for your needed.
Regardless of your solution, I strongly recommend saving the Timestamp for each user answer.
Anyway, instead of storing count, you could use the Timestamp to derive the count as needed. But perhaps deriving this value each time might be too inefficient for you.
If you want to store this information, the count value is effectively a cache for this computation, and based on the usage you described, count is only relevant for a given day.
So adding little metadata might do the trick
|DATE | ID | COUNT | USER_ID |
|20181124| 1 | 3 | 1 |
And access the count for a date and purge the data as needed, or keep indefinitely.
but I need to reset the count column every day. I think I can to that just updating the count column if it's a new answer in a new day
– An j
Nov 25 '18 at 13:37
Only reading the value for today's date effectively invalidates the other data. Since you can easily build the invariant thatcount
is only valid for a specific date/time into your app, you should do so. Think about how your program will scale and how you can test this condition. Also keep in mind that midnight data processing is not instantaneous.
– J W
Dec 3 '18 at 10:35
add a comment |
You can use clockwork and write a method in you're model to update the counter.
place this in /lib/clock.rb in your rails app
require 'clockwork'
require_relative '../config/boot'
require_relative '../config/environment'
include Clockwork
every(1.day, 'Update Counter', at: '00:00') do
User.update_counter
end
and in app/models/user.rb:
def self.update_counter
User.all.each { |user| user.update!(counter: 0)}
end
add a comment |
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
});
}
});
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%2f53463784%2fhow-to-deal-with-data-that-expires-every-day%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
There are many, many solutions to this problem, so you'd have to decide what is acceptable for your needed.
Regardless of your solution, I strongly recommend saving the Timestamp for each user answer.
Anyway, instead of storing count, you could use the Timestamp to derive the count as needed. But perhaps deriving this value each time might be too inefficient for you.
If you want to store this information, the count value is effectively a cache for this computation, and based on the usage you described, count is only relevant for a given day.
So adding little metadata might do the trick
|DATE | ID | COUNT | USER_ID |
|20181124| 1 | 3 | 1 |
And access the count for a date and purge the data as needed, or keep indefinitely.
but I need to reset the count column every day. I think I can to that just updating the count column if it's a new answer in a new day
– An j
Nov 25 '18 at 13:37
Only reading the value for today's date effectively invalidates the other data. Since you can easily build the invariant thatcount
is only valid for a specific date/time into your app, you should do so. Think about how your program will scale and how you can test this condition. Also keep in mind that midnight data processing is not instantaneous.
– J W
Dec 3 '18 at 10:35
add a comment |
There are many, many solutions to this problem, so you'd have to decide what is acceptable for your needed.
Regardless of your solution, I strongly recommend saving the Timestamp for each user answer.
Anyway, instead of storing count, you could use the Timestamp to derive the count as needed. But perhaps deriving this value each time might be too inefficient for you.
If you want to store this information, the count value is effectively a cache for this computation, and based on the usage you described, count is only relevant for a given day.
So adding little metadata might do the trick
|DATE | ID | COUNT | USER_ID |
|20181124| 1 | 3 | 1 |
And access the count for a date and purge the data as needed, or keep indefinitely.
but I need to reset the count column every day. I think I can to that just updating the count column if it's a new answer in a new day
– An j
Nov 25 '18 at 13:37
Only reading the value for today's date effectively invalidates the other data. Since you can easily build the invariant thatcount
is only valid for a specific date/time into your app, you should do so. Think about how your program will scale and how you can test this condition. Also keep in mind that midnight data processing is not instantaneous.
– J W
Dec 3 '18 at 10:35
add a comment |
There are many, many solutions to this problem, so you'd have to decide what is acceptable for your needed.
Regardless of your solution, I strongly recommend saving the Timestamp for each user answer.
Anyway, instead of storing count, you could use the Timestamp to derive the count as needed. But perhaps deriving this value each time might be too inefficient for you.
If you want to store this information, the count value is effectively a cache for this computation, and based on the usage you described, count is only relevant for a given day.
So adding little metadata might do the trick
|DATE | ID | COUNT | USER_ID |
|20181124| 1 | 3 | 1 |
And access the count for a date and purge the data as needed, or keep indefinitely.
There are many, many solutions to this problem, so you'd have to decide what is acceptable for your needed.
Regardless of your solution, I strongly recommend saving the Timestamp for each user answer.
Anyway, instead of storing count, you could use the Timestamp to derive the count as needed. But perhaps deriving this value each time might be too inefficient for you.
If you want to store this information, the count value is effectively a cache for this computation, and based on the usage you described, count is only relevant for a given day.
So adding little metadata might do the trick
|DATE | ID | COUNT | USER_ID |
|20181124| 1 | 3 | 1 |
And access the count for a date and purge the data as needed, or keep indefinitely.
edited Nov 25 '18 at 2:34
answered Nov 25 '18 at 2:05
J WJ W
364
364
but I need to reset the count column every day. I think I can to that just updating the count column if it's a new answer in a new day
– An j
Nov 25 '18 at 13:37
Only reading the value for today's date effectively invalidates the other data. Since you can easily build the invariant thatcount
is only valid for a specific date/time into your app, you should do so. Think about how your program will scale and how you can test this condition. Also keep in mind that midnight data processing is not instantaneous.
– J W
Dec 3 '18 at 10:35
add a comment |
but I need to reset the count column every day. I think I can to that just updating the count column if it's a new answer in a new day
– An j
Nov 25 '18 at 13:37
Only reading the value for today's date effectively invalidates the other data. Since you can easily build the invariant thatcount
is only valid for a specific date/time into your app, you should do so. Think about how your program will scale and how you can test this condition. Also keep in mind that midnight data processing is not instantaneous.
– J W
Dec 3 '18 at 10:35
but I need to reset the count column every day. I think I can to that just updating the count column if it's a new answer in a new day
– An j
Nov 25 '18 at 13:37
but I need to reset the count column every day. I think I can to that just updating the count column if it's a new answer in a new day
– An j
Nov 25 '18 at 13:37
Only reading the value for today's date effectively invalidates the other data. Since you can easily build the invariant that
count
is only valid for a specific date/time into your app, you should do so. Think about how your program will scale and how you can test this condition. Also keep in mind that midnight data processing is not instantaneous.– J W
Dec 3 '18 at 10:35
Only reading the value for today's date effectively invalidates the other data. Since you can easily build the invariant that
count
is only valid for a specific date/time into your app, you should do so. Think about how your program will scale and how you can test this condition. Also keep in mind that midnight data processing is not instantaneous.– J W
Dec 3 '18 at 10:35
add a comment |
You can use clockwork and write a method in you're model to update the counter.
place this in /lib/clock.rb in your rails app
require 'clockwork'
require_relative '../config/boot'
require_relative '../config/environment'
include Clockwork
every(1.day, 'Update Counter', at: '00:00') do
User.update_counter
end
and in app/models/user.rb:
def self.update_counter
User.all.each { |user| user.update!(counter: 0)}
end
add a comment |
You can use clockwork and write a method in you're model to update the counter.
place this in /lib/clock.rb in your rails app
require 'clockwork'
require_relative '../config/boot'
require_relative '../config/environment'
include Clockwork
every(1.day, 'Update Counter', at: '00:00') do
User.update_counter
end
and in app/models/user.rb:
def self.update_counter
User.all.each { |user| user.update!(counter: 0)}
end
add a comment |
You can use clockwork and write a method in you're model to update the counter.
place this in /lib/clock.rb in your rails app
require 'clockwork'
require_relative '../config/boot'
require_relative '../config/environment'
include Clockwork
every(1.day, 'Update Counter', at: '00:00') do
User.update_counter
end
and in app/models/user.rb:
def self.update_counter
User.all.each { |user| user.update!(counter: 0)}
end
You can use clockwork and write a method in you're model to update the counter.
place this in /lib/clock.rb in your rails app
require 'clockwork'
require_relative '../config/boot'
require_relative '../config/environment'
include Clockwork
every(1.day, 'Update Counter', at: '00:00') do
User.update_counter
end
and in app/models/user.rb:
def self.update_counter
User.all.each { |user| user.update!(counter: 0)}
end
answered Nov 26 '18 at 10:01
UdAYUdAY
1888
1888
add a comment |
add a comment |
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.
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%2f53463784%2fhow-to-deal-with-data-that-expires-every-day%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
no. I don't need a simple validation. I need to update the column with the counter and reset every nigh. thanks.
– An j
Nov 25 '18 at 17:52
you can use a background job, which will execute every day and update the column on specific time duration
– rohit
Nov 26 '18 at 6:28