Reputation: 3523
i have a "Job" module (and corresponding table in the db), that module have a field called scheduled_run (datetime) and a field called user_id:
Upvotes: 2
Views: 3207
Reputation: 207
Is it plain ruby or are you using rails or other ORM/framework? I'll assume as much, since the questiontag marks activerecord
It could be done like this:
Job.where("user_id = ? AND datetime = ?", user_id, datetime).all
more within the Rails documentation here http://api.rubyonrails.org/classes/ActiveRecord/Base.html
You could also use date ranges, just dig a bit around the docs ;)
Upvotes: 0
Reputation: 1270
To get all the jobs that belong to a user and are scheduled for today, you'll need to search for all scheduled_runs within the today time period:
user = User.find(1) # Will get the user with ID 1
jobs = Job.where("user_id = :id AND scheduled_run >= :start AND scheduled_run < :end",
:id => user.id,
:start => Date.today,
:end => 1.day.from_now.to_date)
To get all the jobs for last week, we'll do a similar command, but we'll switch :start
and :end
to be the start and end of the week:
user = User.find(1) # Will get the user with ID 1
jobs = Job.where("user_id = :id AND scheduled_run >= :start AND scheduled_run < :end",
:id => user.id,
:start => 1.week.ago.to_date,
:end => Date.today)
In both of the above commands, to_date
will cause ActiveRecord
to treat the resulting DateTime
as beginning of day.
Upvotes: 1
Reputation: 160551
Something like:
Job.where(:user_id => some_user_id, :scheduled_run => Date.today).all
To find the ones for last week you'll need to specify a date range. I'll let you figure that part out. You'll want to use a "Range condition" or "Subset condition" mentioned in the ActiveRecord guide.
The Rails ActiveRecord Guide is good to read. Also, see the "Conditions" section in the ActiveRecord doc.
Upvotes: 1