Rails ActiveRecord Date Search

It's hard for me to understand this, but how can I say that the finder instruction ignores the time of the Datetime field in db?

def trips_leaving_in_two_weeks Trip.find(:all, :conditions => ["depart_date = ?", 2.weeks.from_now.to_date]) end 

I want date_date to be returned as soon as the date, but it also returns the time and prevents this equality from working. Is there anything that can be compared with dates? Thanks

Edit

Here the code I'm using now works:

 Trip.find(:all, :conditions => ["DATE(depart_date) = ?", 2.weeks.from_now.to_date]) 
+6
sql ruby-on-rails activerecord
source share
2 answers

Not sure which database you are using, but does it work?

 "depart_date = DATE(?)" 
+7
source share

I would use this approach:

Rails 3.x

 Trip.where( :depart_date => 2.weeks.from_now.beginning_of_day..2.weeks.from_now.end_of_day ) 

Rails 2.x

 Trip.all( :conditions => { :depart_date => 2.weeks.from_now.beginning_of_day..2.weeks.from_now.end_of_day }) 

If you index the depart_date column, this solution will be effective since the query uses the index. This solution is DB neutral.

When calculated fields are used in the where clause, performance degrades (unless there is a special index).

+4
source share

All Articles