Title: Display the closest future time to now from database
Tags: ruby-on-rails;ruby;date;time
Question: In my Ruby on Rails application I want to be able to display the next future time in the database that is the closest to the current time, e.g., in the database if there are records with the times 13:00, 13:30, 18:00, 19:00 etc.  And, if the time currently is 13:48 the time 18:00 will be displayed. I have a similar thing working with the date:
<% film.showings.take(1).each do |showing| %&gt;
<%= film.showings.select{|s| s.show_date &gt;= Date.today}.sort_by(&amp;:show_date).first.show_date.strftime("%A %e %B %Y") %&gt;
<% end %&gt;
But attempting to adapt this with the time:
<% film.showings.take(1).each do |showing| %&gt;
<%= film.showings.select{|s| s.show_time &gt;= Time.now}.sort_by(&amp;:show_time).first.show_time %&gt;
<% end %&gt;
This does not work and I get the following error:
undefined method `show_time' for nil:NilClas
which occurs because of s.show_time &gt;= Time.now. If I change this to s.show_time = Time.now the error goes but then it just displays the current time and not the time from the database.
It is worth noting that the show_date and show_time are in the same record, for example the first record could have a date of 26/01/2015 and a time of 22:00. What I want is to display the time of records where the date is in the future and show that date's first time, as could have two records in the database with a date of 26/01/2015 and one with a time of 22:00 and another with a time of 22:30.
Can anyone help?
Comment: It would be worth explaining how you are storing your times. `Time.now` returns a `Time` object but this includes date information too. If you are only storing the hours and minutes in the database then you could well be comparing that time in 1970 or 2000 to the current time in which case it will obviously not be in the future.

Comment: Additionally `s.show_time = Time.now` is setting the time to the current time not testing if it *is* the current time because you are using `=` instead of `==`. It also explains why you aren't getting the time from the database as you've just set it to `Time.now` instead.


Here is another answer: It's very strange code.
But I think it doesn't find suitable records.
Try this:
<% film.showings.take(1).each do |showing| %&gt;
<%= film.showings.select{|s| s.show_time &gt;= Time.now}.sort_by(&amp;:show_time).first.try(:show_time) %&gt;
<% end %&gt;
If it doesn't crash, I'm right.
Comment for this answer: It doesn't crash, but it doesn't display anything?
Comment for this answer: I have records in the database with the time of 22:00, so it should work
Comment for this answer: Any ideas of what to do?
Comment for this answer: Sorry I'm new to ror, what do you mean split expressions?
Comment for this answer: So you doesn't have records where `s.show_time >= Time.now`
Comment for this answer: What is their date? it tests not only time, but a date too
Comment for this answer: Split expressions and use debugger
Comment for this answer: a.b.c.d => e1=a.b; e2=e1.c; e3=e2.d and look for each result

Here is another answer: In your example, you loading the entire database into memory, then filtering it to find the row with the time you want. I'd let the database do all the work instead.
film.showings.where("show_time &gt;= ?", Time.now).order(show_time: :asc).first
That doesn't answer your actual original problem where it appeared there were no suitable times even though you think there should be. You may or may not still have this problem with this approach -- if you do, debugging it will be different than debugging your "all in memory with ruby" original approach, so I'd start out with this, then debug by looking at the SQL generated, etc.
On the other hand, if you only expect a dozen or so results to filter, maybe your approach is fine too.
Additionally, if every row in your database has one date and one time -- I'd change your database schema to use a combined date+time value in one column. Ordinarily this is the best way to go. For a movie database, I could see separating date and time might make sense -- but if every row has ONE date and ONE time already, then there's no disadvantage to combining them as a datetime, and that's what I'd do. I suspect your bug may go away if you do that -- you should be able to do it with separate date and time, sure, but combined makes more sense, and will be less confusing to deal with in ruby too.
Comment for this answer: Where are you suggesting to place that code? I currently do not have a method in my controller for this so should I create one for that code? I also do not have many times in the database, and would prefer to keep date and time separate so that a user can eventually add one without adding the other. Thanks for your response!
Comment for this answer: I don't know where to place it, wherever you want, wherever you have your code already?  Keeping date and time separate could be reasonable. If you don't want to let the database do the work for you, then you can go back to debugging your code. You'll just have to debug it; it's odd code though; you probably want to go through each piece and make sure you understand it, did you just copy and paste it from somewhere?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:01/26/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ruby-on-rails><ruby><date><time>', 'Title': 'Display the closest future time to now from database', 'ViewCount': 477, 'id': 28157788, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}