Ruby 日期比较
@student = Student.find(:all, :conditions => {:school_id => school.id,:created_at => Date.today })
我需要编写这个 ruby 查询来检索学校中的所有学生,其中学生的创建日期应该是同一天。
但这给出了 nil 值。当我使用“if”条件时,我可以使用 ifcreated_at.to_date == Date.today.to_date
。
所以我的问题是如何将日期时间格式的符号(:created_at)转换为日期格式。 (意味着,这里 Student.find(.., :created_at.to_date => Date.today.to_date) 是不可能的,替代方法是什么)
@student = Student.find(:all, :conditions => {:school_id => school.id,:created_at => Date.today })
I need to write this ruby query to retrieve all students in a school which students date of creation should be the same day.
But This giving nil values.When I use "if" condition, I can use if created_at.to_date == Date.today.to_date
.
So my question is how I can convert the datetime formatted symbol(:created_at) to date format.
(means, Here Student.find(.., :created_at.to_date => Date.today.to_date) is not possible,What is the alternate way)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
部分技巧是
created_at
是时间值,而不是日期值。 此建议解决此问题的一种方法StackOverflow 对类似问题的回答是在查询期间将created_at
值转换为日期:(请注意,我重构了查询以使用学校的
has_many :students
关系。)Part of the trick is that
created_at
is a time value, not a date value. One way of solving this problem, recommended by this StackOverflow answer to a similar question, is to convert thecreated_at
value to a date during the query:(Note that I refactored the query to use the school's
has_many :students
relationship.)