activerecord comparing times

3.8k Views Asked by At

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)
1

There are 1 best solutions below

4
On BEST ANSWER
@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 ...)