5

What am I doing incorrectly that is giving me a blank array from this command?

Item.where(:load_date => Date.today + 2)

Here is my Rails Console:

.9.3-p194 :024 > Item.first.load_date
Item Load (0.3ms)  SELECT "items".* FROM "items" LIMIT 1
=> Fri, 24 May 2013 
1.9.3-p194 :025 > Item.where(:load_date => Date.today + 2)
Item Load (0.5ms)  SELECT "items".* FROM "items" WHERE "items"."load_date" = '2013-05-24'
=> []
1.9.3-p194 :026 > Item.first.load_date == Date.today + 2
Item Load (0.3ms)  SELECT "items".* FROM "items" LIMIT 1
=> true 

Item Model:

... 
#  load_date       :date
...

class Item < ActiveRecord::Base
attr_accessible :bt_num, :dept, :formula, :item_code, :load_date, :prod_comments, :qc_comments, :qc_tech, :qty_in_kg, :qty_in_liters, :rm_ok_by, :series, :status, :time_to_produce, :vat
...
xdazz
  • 158,678
  • 38
  • 247
  • 274
collenjones
  • 510
  • 5
  • 19

1 Answers1

1

Try these

Item.where(:load_date => (Date.today + 2).strftime)
  or 
Item.where("Date(load_date) =?", (Date.today + 2).strftime)
  • The second one works but not the first (first still returns blank). Why would the formatting of the date matter?? And why wouldn't the first work?? – collenjones May 22 '13 at 09:08
  • load_date is the column of datetime but we want only Date from there so I used Date(load_date).... and strftime used because it will convert (Date.today+2) to the format "2013-05-24" and in database values are saved as "2013-05-24 12:12" instead of "fridat May 24, 2013 " – Harish Malik May 22 '13 at 10:53
  • Why wouldn't Item.first.load_date show me "2013-05-24 12:12" instead of "Fri, 24 May 2013"? When I check the class of Item.first.load_date, it is also 'Date'. I appreciate the help, I'm just trying to understand what's going on. – collenjones May 22 '13 at 15:22
  • Then try this... Item.where("load_date=?", (Date.today + 2).strftime) – Harish Malik May 23 '13 at 06:49