2

i am trying to do a query that will be compare the time stored in the database to the current time, and if it is greater than today to display those records.

below is query i am currently using that isnt displaying any records

@schedules = Schedule.where(:team_id => current_user[:team_id], :event => '1', :time => ">= Time.now.zone")

how do i go back query against a timestamp? so that these records will be displayed?

have also tried the following

@schedules = Schedule.find_all_by_team_id_and_event_and_time(current_user[:team_id],"1", :time)
Boss Nass
  • 3,384
  • 9
  • 48
  • 90

1 Answers1

3
@schedules = Schedule.where("team_id = ? and event = ? and time >= ?", [current_user[:team_id], "1", Time.zone.now])

The string is used directly in the SQL query so you need to make sure the column names are correct and unambiguous (if you joined on another table that also has a team_id colum, you would need to do schedules.team_id = ? and ...)

deefour
  • 34,974
  • 7
  • 97
  • 90
  • our time is stored like 'Mon, 06 Aug 2012 01:09 am' which i think is causing some issues, if there a way to compare this to the current time? – Boss Nass Aug 12 '12 at 04:56
  • Why is your time stored that way? Is it not a timestamp or datetime field? If it's just a string the SQL is more complex to convert the string to a date for comparison. Dates/times should really be stored in a column with a type the rdbms interprets as date/time. – deefour Aug 12 '12 at 04:59
  • hi deefour it was store that way so that when we used the jquery time/date picker it would save – Boss Nass Aug 12 '12 at 05:03
  • i have changed our field time to timestamp, though the issue I have now is that I cannot render it to be displayed nicely – Boss Nass Aug 12 '12 at 12:36