Rails ActiveRecord Query Date Range - ruby-on-rails

I'm trying to use the following line in my controller to capture all tasks due less than a week from the current date:
#due_this_week = current_user.tasks.where(due_date: Date.today..1.week.from_now)
For some reason it's not finding any results even I know I have tasks due within four and six days. This is the only instance variable using a range query. I have another one that works fine to find overdue tasks:
#overdue = current_user.tasks.where("due_date <= ?", Date.today)
What am I missing?

Should be:
#due_this_week = current_user.tasks.where(due_date: 1.week.ago..Date.today)

FYI you can also use one sided ranges.
For all calls with due date from 1 week ago
#due_this_week = current_user.tasks.where(due_date: 1.week.ago..)
or for all calls with due date up to today
#due_this_week = current_user.tasks.where(due_date: ..Date.today)

Turns out my controller somehow wasn't set up correctly and was no longer saving the current_user's ID when creating new assignments, which is why they weren't being found. I figured this out using the rails console and running a find on the last few assignments submitted. The user_id was set to nil. Thanks for the assist #mu is too short.

Related

Need help on active record updating via rails

I had the follow code in a rake file that I will be run weekly.
now = Date.today
Order.where(("status NOT IN ('Completed','Canceled','Shipped') AND DATE(updated_at) <= ?"),(now-30)).update_all("status = '*'",'Pending Requestor')
The problem is it is throwing wrong number of arguments error.
looking at http://apidock.com/rails/ActiveRecord/Base/update_all/class
I tried
now = Date.today
Order.update_all("status = 'Pending Requester'",("status NOT IN ('Completed','Canceled','Shipped') AND DATE(updated_at) <= ?"),(now-30))
but that gives me a 3 for one error.
So what I need to do is I need to find all of the orders where the status is not in that list and the last time they were updated was beyond 30 days ago and automatically put them into a Pending Requester status.
Can someone help me with what I am getting wrong on this?
In your code, what is assigned to the variable now? I'm going to assume it is Time.now.
Also, all of the extra parenthesis you added aren't necessary. I've simplified your query below and wrote it out so that it is easy to understand.
Change your code to:
Order.where(
"status NOT IN (?) AND updated_at <= ?", # Simplifyied the query
%w(Completed Canceled Shipped), # Can also be written as ['Completed', 'Canceled', 'Shipped']
30.days.ago # Self-explanatory
).update_all(status: 'Pending Requestor')
where only takes 1 argument UNLESS the argument contains question marks (?). For each question mark, it receives an additional argument to substitute the question mark with a value.
Bonus: When working with statuses in Rails, I suggest learning the Enumerable convention. It's amazing!

Filter issues updated by particular user in period of time using JQL

Is there any way to find all issues updated by particular user in particular time period in every day by using JQL or is there any plugin to solve this?.
If by updated you mean change of status you can chack something like this:
status changed by "user.name" and updated > startOfDay("-1")
Of course the start of the day -1 shows everything updated since yesterday, but you can also go with hours.
You could run a query like this:
reporter = usernameGoesHere AND created > startOfDay() and created < "2015/06/18 15:00"
So I had one ticket that I created at 2:20. Running this query gave me all issues created from the start of today and then queried out the results that were created before 6/18 at 3:00PM
Let me know if this helps you out.
You can also look at the Advanced Filtering page for JIRA
assignee = 'your user' AND updated > startOfDay()
You can create a filter which will update daily.

How can i remove the milliseconds from date time in rails?

I have to compare the date in rails to get the values after that date I have send the date as "2013-03-04T06:26:25Z"but actually the record in the db contains date as follows 2013-03-04 06:26:25.817149 so when i check with the date it also returns that record but i want records after that date. how can i remove the milliseconds from the db? please help me.
I had a similar problem
Update your time object like this before sending it to the database :
time.change(:usec => 0)
Since ruby 1.9 you can use round
t = Time.now.round
t.usec # => 0
I was also having this problem, however when I was applying the change as indicated in #Intrepidd's answer, it wasn't affecting the microseconds of my time object.
Please note that (at least currently) the :usec key only works with the Time#change method, and not with the DateTime#change method.
The DateTime#change method ignores the keys that it doesn't accept, so you wouldn't be able to tell that your attempted change of the microseconds didn't work unless you inspected the object further (such as with DateTime#rfc3339(9)).
So before you attempt this change, make sure that you are working with a Time object, not a DateTime object.

datetime_select with zeroed minutes

I know that I can use the component parts of the date helpers, rather than the full
datetime_select, but I'm not sure how it would work as far as combining the params.
A bit of background, I'm creating an app for traffic monitoring where people can log traffic counts in 1 hour blocks. I'm presenting the user with a datetime_select so they can specify the start of the block, then later I'm calculating the end.
So I don't want people to be able to submit minutes or seconds, well seconds aren't shown with the helper so that's a start.
I've tried to zero it before the record is created with something like:
params[:result]['start(5i)'] = 0
which is the key that the development log shows rails is using for minutes. Unfortunately I get:
undefined method `empty?' for 0:Fixnum
I guess I could do this with some javascript, hide the minutes select box and remove all but the "00" option. I'd rather find a nice, clean solution if I can though.
Grateful for some tips. Happy to provide more information but not sure what else might be of use at the moment.
params are Strings! try this:
params[:result]['start(5i)'] = '0'
or this:
params[:result]['start(5i)'] = ''

fullcalendar with rails - limit results to a range

I've got fullcalendar working with a small rails app (yeah) but it's sluggish because the find in my controller is finding ALL the records before it renders the calendar. I'm using a JSON approach. The field names I'm using are starts_at and ends_at. This (in the index method of the assignments_controller) works:
#assignments = Assignment.find(:all, :conditions => "starts_at IS NOT NULL")
But, as I said, it's pokey, and will only get worse as more records get added.
So this is clearly more of a rails question than a fullcalendar question: I can't figure out how to get fullcalendar to initially display the current week (when no parameters have been sent) and then accept parameters from next/previous buttons while, in either case, only looking up the relevant items from the database.
Oh - this is rails 2.x, NOT 3.
Thanks for any pointers.
Please ignore this question.
It turned out to be an issue with Date format inconsistencies between JavaScript (Epoch) and Ruby. At least that's what I think at the moment.
I'm still scratching my head, trying to figure out how exactly I "fixed" it, but it seems to be working.
I was aware of this project: http://github.com/bansalakhil/fullcalendar
but it took me ages to get the nuance of Time.at figured out.
I must say, Time is a tricky thing.
In real life as well as in code.
Thanks to everyone who gave my (misguided, as it turned out) question a glance.

Resources