显示从数据库到现在最近的未来时间 [英] Display the closest future time to now from database

查看:166
本文介绍了显示从数据库到现在最近的未来时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Ruby on Rails应用程序中,我希望能够在数据库中显示与当前时间最接近的未来时间,例如在数据库中,如果有时间13:00,13: 30,18:00,19:00等。如果现在的时间是13:48,那么18:00的时间就会显示出来。我有一个与日期相似的事情:

 <%film.showings.take(1).each do | shows | %> 
<%= film.showings.select {| s | s.show_date> = Date.today} .sort_by(&:show_date).first.show_date.strftime(%A%e%B%Y)%>
<%end%>

但是尝试适应这个时间:

 <%film.showings.take(1).each do | shows | %> 
<%= film.showings.select {| s | s.show_time> = Time.now} .sort_by(&:show_time).first.show_time%>
<%end%>

这不行,我收到以下错误:

 未定义的方法`show_time'为nil:NilClas 

因为 s.show_time> = Time.now 。如果我将这个更改为 s.show_time = Time.now ,那么错误就会显示当前时间,而不是数据库的时间。



值得注意的是,show_date和show_time在同一条记录中,例如第一条记录可能有26/01/2015的日期和22:00的时间。我想要的是显示日期在未来的记录时间,并显示该日期是第一次,数据库中可能有两个记录,日期为26/01/2015,一个记录的时间为22:00另一个时间是22:30。



任何人都可以帮忙?

解决方案>

在您的示例中,将整个数据库加载到内存中,然后过滤以查找所需时间的行。我会让数据库做所有的工作。

  film.showings.where(show_time> =?, Time.now).order(show_time::asc).first 

没有回答你的实际原来的问题出现在那里没有适合的时候,即使你认为应该有。你可能或可能不会有这个方法的这个问题 - 如果你这样做,调试它将不同于调试你的所有在内存与红宝石的原始方法,所以我从这开始,然后通过查看调试SQL生成等。



另一方面,如果您只希望十几个结果过滤,也许您的方法也很好。



此外,如果数据库中的每一行都有一个日期和时间 - 我将更改数据库模式,以便在一列中使用组合的日期+时间值。通常这是最好的方法。对于电影数据库,我可以看到分隔的日期和时间可能是有意义的 - 但是如果每一行都有一个日期和一个时间,那么将它们组合成一个日期时间就没有任何缺点,这就是我所做的。我怀疑你的错误可能会消失,如果你这样做 - 你应该能够分开的日期和时间来做,但是组合更有意义,并且会更少的混乱处理红宝石也是。


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| %>
    <%= film.showings.select{|s| s.show_date >= Date.today}.sort_by(&:show_date).first.show_date.strftime("%A %e %B %Y") %>
<% end %>

But attempting to adapt this with the time:

<% film.showings.take(1).each do |showing| %>
    <%= film.showings.select{|s| s.show_time >= Time.now}.sort_by(&:show_time).first.show_time %>
<% end %>

This does not work and I get the following error:

undefined method `show_time' for nil:NilClas

which occurs because of s.show_time >= 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?

解决方案

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 >= ?", 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.

这篇关于显示从数据库到现在最近的未来时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆