Torstein
Torstein

Reputation: 397

Why does not querying for a date return anything?

I have a model with a dep_date field, which is a date (not datetime). When querying the model for records with a given date, nothing is returned. Using RoR 3.2 with SQLite

1.9.3-p327 :019 > Flight.find(62)
  Flight Load (0.4ms)  SELECT "flights".* FROM "flights" WHERE "flights"."id" = ? LIMIT 1  [["id", 62]]
 => #<Flight id: 62, dep_city_id: 3, arr_city_id: 15, dep_date: "2013-03-28", dep_time: nil, price_per_adult: #<BigDecimal:b7e3c94,'-0.0',9(9)>, price_per_child: #<BigDecimal:b7e3c30,'-0.0',9(9)>, free_seats: nil, flight_status: nil, created_at: "2013-03-14 22:15:48", updated_at: "2013-03-14 22:15:48", arr_date: "2013-04-15", arr_time: nil, c_flight_time: 0, c_flight_distance: 0> 

1.9.3-p327 :020 > Flight.where(:dep_date => "2013-03-28")
  Flight Load (0.5ms)  SELECT "flights".* FROM "flights" WHERE "flights"."dep_date" = '2013-03-28'
 => [] 

Upvotes: 0

Views: 55

Answers (2)

Torstein
Torstein

Reputation: 397

This was caused by SQLite3. Querying on dates works fine with PostgreSQL.

Upvotes: 0

rocket scientist
rocket scientist

Reputation: 2447

My first guess would be that Rails is interrupting "2013-03-28" as a string and not as a date. I would suggest trying this:

Flight.where(:dep_date => "2013-03-28".to_date)

Upvotes: 1

Related Questions