Ruby on Rails - Railscast#213日历排序按日期时间不是日期 [英] Ruby on Rails - Railscast #213 Calendar sort by datetime not date

查看:290
本文介绍了Ruby on Rails - Railscast#213日历排序按日期时间不是日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个涉及日历的应用程式。我发现,建立一个日历的铁轨,虽然日期证明是非常有益的。我有一切正常运行我只是试图允许每个日期的内容由日期时间组织,而不只是日期,因此它们按正确的时间顺序出现。

I am building an app that involves a calendar. I found that the railscast for building a calendar although dated proved to be very helpful. I have everything up and running I am just trying to allow for the contents on each date to be organized by the datetime and not just the date so they appear in the right chronological order.

我的目前控制器如下:

def index
  @lessons = Lesson.all
  @lesson_by_date = @lessons.group_by { |i| i.lesson_date.to_date }
  @date = params[:date] ? Date.parse(params[:date]) : Date.today
end

将非常感谢。感谢

推荐答案

我假设你的问题是如何组织我的@lessons由datetime。有几个不同的场景我会提到。我有点有限,因为我不知道从你的帖子你是如何组织架构的桌子,但我认为控制器给我一个大棒的想法。

I'm going off the assumption your question is how do I organize my @lessons by datetime. There are a few different scenarios I'll mention. I'm a bit limited since I don't know from your post how you've organized your schema for the table, but I think the controller gives me a ballpark idea.

1)
您应该替换:

1) You should replace:

@lesson = Lesson.all
@lesson_by_date = @lessons.group_by { |i| i.lesson_date.to_date }

使用更快的东西:

@lesson_by_date = Lesson.order('lesson_date')


$ b b

2)
如果你想知道为什么你当前的代码不工作,因为你的group_by块试图将i.lesson_date转换为一个日期IE to_date。

2) If you're wondering why your current code isn't working it's because your group_by block is trying to convert i.lesson_date to a date IE to_date. Instead you may want to try to_datetime.

3)我会查看 http://apidock.com/rails/ActiveRecord/QueryMethods/order

这篇关于Ruby on Rails - Railscast#213日历排序按日期时间不是日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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