Issue counting using conditional datetime - ruby-on-rails

I'm trying to count all my policies that are'n expired >= that today
date_end >= TODAY
Here is my table
|policies|
|id| |num| |date_ini| |date_end|
1 12484 2013-04-01 2014-05-01
2 41511 2012-04-01 2013-05-01
3 14441 2013-05-01 2014-06-01
There are 2 values that aren't expired
Here is my controller
#policies =Policy.count(:conditions =>['date_end >= Date.now'])
I tried
#policies =Policy.count(:conditions =>['date_end >= Date.today'])
And also
#policies =Policy.count(:conditions =>['date_end >= curtime()'])
Please somebody can help me?
I will really appreciate help

It's not working because Date.today is inside a String, so it isn't being evaluated as an actual Date. You probably also want to use Date.current instead, to take the configured time zone into account.
Try this instead:
#policies = Policy.count( :conditions => ['date_end >= ?', Date.current] )

If date_end is stored as a DATE type of attribute, you can do this:
Policy.where("date_end >= ?", Date.today).count
or if it's stored as a string, just convert the Date.today to a string:
Policy.where("date_end >= ?", Date.today.to_s).count

The problem is that when you call out Date.today, you are returning the format "Fri, 15 Nov 2013" which will not compare to "2014-04-02". To get them in the same format, use
Date.today.to_s

Related

upcoming 3 day from today b/w two dates rails currently it calculate only for today

def self.leave_day
self.where("? BETWEEN start_date AND end_date", Date.today).where(status: "Approved").count
end
but i want to calculate from today to next 3 days .Now it is calculate b/w two dates where status is today if give range of dates (Date.today +1)..(Date.today + 3.days) it is not accept
What do you mean with a range is not accepted? U could do it with a range and set the start_date to the beginning of the date and the end_date to the end of the day.
def self.leave_day
self.where(start_date: (Date.today.beginning_of_day)..((Date.today + 3.days).end_of_day), status: 'Approved').count
end
It's possible to use multiple params on where.
I think it should be like this :
def self.leave_day
self.where("start_date >= ? AND end_date <= ?", Date.today, Date.today + 3.days).where(status: "Approved").count
end
Source : https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-where

Comparison of datetime in where condition

I need to add a date comparison in a query.
The field is column delivery_date timestamp without time zone
The condition should be delivery_date <= today.
I have tried :
"delivery_date < ?", Date.today
delivery_date.lt(Date.today)
"delivery_date" <= time.now [Error : NameError - undefined local variable or method `time' for ]
"delivery_date" <= Time.now [Error : ArgumentError - comparison of String with DateTime failed: ]
but I am getting different errors with all.
Here is the query where I need to add my condition :
datas: tab.project
.active
.where("delivery_date" <= date.today, step_id: Step::OPENED, test: {typess: type})
.joins(:test, :account)
.group(:'account.name')
.order('count(tab.id) DESC')
.count(:id)
Any idea how it should be done?
Try:
tab.project.active.where("delivery_date <= ?", Date.today)
or
tab.project.active.where("delivery_date <= :date", date: Date.today)
In both cases we use Date.today that returns (suprisingly :-) ) the date of today. We use two ways of adding param to a query -
array condition where ? is replaced by next where arguments (in this case the Date.today)
placeholder condition where a symbol is replaced by a hash value for this symbol (date: Date.today)

ActiveRecord multiple where date range

Is there a more ActiveRecord idiomatic way to find which records have a start_at or end_at within a certain date_range? (Basically, need to find the records that start or end in a given time frame). Here's what I'm currently doing:
Project.where('(start_at >= ? AND start_at <= ?) OR (end_at >= ? AND end_at <= ?)', start_at, end_at, start_at, end_at)
You can pass ranges to where and use or:
time_range = (start_at..end_at)
Project.where(start_at: time_range).or(Project.where(end_at: time_range))
For Rails 4 you could use BETWEEN and hash params:
Project.where(
"(start_at BETWEEN :start_at AND :end_at) OR (end_at BETWEEN :start_at AND :end_at)",
start_at: start_at.beginning_of_day, end_at: end_at.end_of_day)

Compare time in updated_at and Time.now

Hi I'm using Rails 3 and want to get all rows that is updated in the last 30 minutes.
This is my current code:
Post.find(:all, condition: { updated_at: Time.now..Time.now-30.minutes })
But it returns every single rows, I also have tried this:
Post.find(:all, condition: [
"updated_at between ? AND ?", Time.now, Time.now - 30.minutes
])
It still return all rows.
I think the problem lies in the format difference.
updated_at format is like 2014-01-26T17:22:52+08:00
While Time.now format is like 2014-02-10 14:39:40 +0800
Any solution? Thanks
Post.where('updated_at > ?', 30.minutes.ago)
You can also try this:
Post.where('updated_at > ?', Time.now - 30.minutes )

Rails activerecord: query for record by a datetime field?

I'm trying to query for records in the TimeSlot table by the field start_date which has type datetime. Here's what I've tried so far that's failed:
TimeSlot.where(:start_date => DateTime.new(2010, 9, 1))
TimeSlot.where(:start_date => DateTime.new(2010, 9, 1).to_s)
TimeSlot.where(:start_date => "2010-09-08")
TimeSlot.where(:start_date => "2010-09-08 00:00:00")
I'd appreciate any help.
Your queries look good to me.
Are you sure that you have a matching row in the db?
To debug, look in your logs/development.log file.
Added:
Problem could be timezones. Your query is using your server's timezone. Your data could be stored in a different timezone.
I'm betting it is a timezone thing as well. Everything in the DB is automatically converted to UTC by rails. Queries 1 and 4 should work if there isn't an offset.
Answer from rubyonrails.org
Client.where("created_at >= :start_date AND created_at <= :end_date",
{:start_date => params[:start_date], :end_date => params[:end_date]})
or
Client.where("created_at IN (?)",
(params[:start_date].to_date)..(params[:end_date].to_date))
Here is what I have for query all the TimeSlot start at "2010-09-08", if your start_date is a date field.
TimeSlot.where("start_date >= ? AND start_date <= ?", "2010-09-08", "2010-09-08")
If you start_date is a datetime field.
TimeSlot.where("start_date >= ? AND start_date <= ?", "2010-09-08", "2010-09-09")
Because the datetime start at 00:00:00

Resources