Compare a DateTime to the current date - ruby-on-rails

I am trying to use a condition on events when the start_at DateTime is equal to or greater than Today's date.
I want to list upcoming events, but clearly they are not upcoming if they have already passed.
I have:
#appointments = Event.find(:all, :conditions => ['move_id = ? AND start_at = ?', #move.id, Date.today])
I think I may be comparing apples and oranges here. It doesn't throw and error, just doesn't do what it is supposed to.
Help! Thanks in advance.

Try:
#appointments = Event.find(:all, :conditions => ['move_id = ? AND start_at >= ?', #move.id, DateTime.now])
Weird is that I can't find DateTime#now documentation.
I'm using Postgres for this test.
>> Event.last.created_at.class
=> Time
>> Event.find(:first, :conditions => ['created_at >= ?', DateTime.now]).valid?
=> true
Migration field code
t.datetime "created_at", :null => false

as you said, start_at is equal or greater than today's date, perhaps it was a problem with the operator that was checking your event's start_at with today's date
Evento.find(:all, :conditions => ['start_at >= ?', DateTime.now])
if you're using mysql you could use "start_at >= CURRENT_DATETIME" or something like this also

Related

ruby check if current date is within date records

Let's say I have a model called Absence that looks something like this
:id => :integer,
:employee_id => :integer,
:start_date => :date,
:end_date => :date
I need to check if an Employee is away today, and return true if they are. Someone is away if they have an absence record that
Has a start date is today or before today,
Has an end date that is either null, or today or ahead of today.
so I need a method on the Employee that is something like
def is_away
?????
end
please help!
I would do something like this:
# add this to absence.rb
def covers_today?
(start_date..end_date).cover?(Date.today)
end
# add this to employee.rb
def away?
absences.any?(&:covers_today?)
end
After doing this just call away? on an employee:
#employee.away?
# => true, if employee has an absense that covers the current date
Assuming that Employee has_many :absences, this should work:
def away?(date = Date.today)
absences.where('start_date <= :date AND (end_date >= :date OR end_date IS NULL)', date: date).exists?
end
You can try this too.
def is_away?
(start_date <= Date.today) and (end_date.nil? or end_date <= Date.today) ? true : false
end

Activerecord syntax of where clause on joins

I want to select all Line items that have an order that has been created between two dates (order has the column date)
Here's what im trying to do
LineItem.where(:product_id => self.id).joins(:order).where(:orders => {"date > ? and date < ?", date_start, date_end}).all
I cant figure out the syntax of the last condition...
I know this kind of where do work:
.where("date > ? and date < ?", date_start, date_end)
and this kind of where too:
.where(:orders => {:id => 23043})
but how can i do a mix of the two kinds so i can get something like the first request?
Try this
.where(:date => date_start..date_end, :order => {:id => 23043})
You can just chain the clauses together and it will automatically join them with an AND:
.where("date > ? and date < ?", date_start, date_end).
where(:orders => {:id => 23043})

How to scope last week by Date object [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Scoping date attribute for this week?
I am trying to scope all of my Products for this week, so it should show all the products leading up to whichever day of the week.
class Product < ActiveRecord::Base
attr_accessible :purchase_date
def self.last_week # All prices of last week.
where(:purchase_date => 1.week.ago)
end
create_table :products do |t|
t.date :purchase_date
end
end
This code renders nothing in the view though so what do I need to correct?
ANSWER
For some reason I had to add advance(:days => -1) to in order to also retrieve Monday as well. You may not have to do this though.
def self.last_week
where(:purchase_date => 1.week.ago.beginning_of_week.advance(:days => -1)..1.week.ago.end_of_week).order("purchase_date desc")
end
UPDATED ANSWER
I had to do the advance(:days => -1) because of the Time zone I am in. I got rid of this by making sure I'm in my own Time zone. So now it can be normal as it should be:
def self.last_week
where(:purchase_date => 1.week.ago.beginning_of_week..1.week.ago.end_of_week)
end
And it should work correctly ONLY if you go by the default Rails Time zone or you config your own:
app/config/environment/development.rb
config.time_zone = "Eastern Time (US & Canada)"
Good luck.
This should do the trick:
scope :last_week, lambda { where("purchase_date >= :date", :date => 1.week.ago) }
scope :past_week, lambda { where("purchase_date >= :start_date AND purchase_date <= :end_date", {:start_date => 1.week.ago, :end_date => 1.day.ago }) }

compare datetime in find conditions

I am trying to get all users that are updated maximum 90 seconds ago:
User.find(:all, :include => { :core => :image },
:conditions => ["updated_at > ?", Time.now - 90.seconds] )
But it doesn't work.
why?
how can i do?
thanks
If you set config.time_zone in your environment.rb to anything other than UTC, you need to do
User.find(:all, :include => { :core => :image },
:conditions => ["updated_at > ?", Time.now.utc - 90.seconds] )
I'm going to assume that image is an attribute of an association called core?
Do you need to specify users.updated_at
It can be time zone problem. Try to find out Time.now - 1.day, if it'll work, so set your Time.zone in application controller.
And second, you can try your construction without :include option if it cause your problem

Ruby on Rails query by datetime range ( last 24, 48, etc... hours )

I'm trying to build a query that will search for recent entries based on column 'last_login_at'. This is a datetime field with time zone (i.e. Time.zone.now)
When I execute
User.find(:all, :conditions => ["last_login_at < ?", 24.hours.ago])
I get nothing.
Alternatively I can define today as Time.zone.today and yesterday as Time.zone.today - 1.day
and run
User.find(:all, :conditions => ["last_login_at between ? and ?", today, yesterday])
and it still returns 0 results. I know there are some entries that fall into this category.
Old question, but here's newer syntax:
User.where(last_login_at: 3.days.ago..DateTime.now)
Ensure that the timezones you have recorded within the database and those your rails app is outputting are equal. This can sometimes cause an issue. Otherwise try this named scope:
class User < ActiveRecord::Base
named_scope :last_loggedin_before, lambda { |time_ago| { :conditions => ['last_login_at < ?', time_ago] } }
named_scope :last_loggedin_within, lambda { |time_ago| { :conditions => ['last_login_at > ?', time_ago] } }
end
allowing you to do:
User.last_loggedin_before 24.hours.ago
or
User.last_loggedin_within 24.hours.ago

Resources