Rails:使此查询与数据库无关......?
我的模型中有这两行是为 PostgreSQL 编写的:
named_scope :by_month, lambda { |month| { :conditions => ["EXTRACT(MONTH FROM recorded_on) = ?", month] }}
named_scope :by_year, lambda { |year| { :conditions => ["EXTRACT(YEAR FROM recorded_on) = ?", year] }}
我在生产中运行 PostgreSQL,但我正在使用 SQLite3 进行开发。 如何以与数据库无关的方式编写这些行?
顺便说一句,“recorded_on”由以下内容组成:
Model.recorded_on = Time.parse("Fri, 01 May 2009 08:42:23 -0400")
I have these two lines in my model, written for PostgreSQL:
named_scope :by_month, lambda { |month| { :conditions => ["EXTRACT(MONTH FROM recorded_on) = ?", month] }}
named_scope :by_year, lambda { |year| { :conditions => ["EXTRACT(YEAR FROM recorded_on) = ?", year] }}
I'm running PostgreSQL in production, but I'm developing with SQLite3. How can I write those lines in a way that is database-agnostic?
Btw, "recorded_on" is formed from the following:
Model.recorded_on = Time.parse("Fri, 01 May 2009 08:42:23 -0400")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BETWEEN 应该是非常通用的:像这样怎么样:
如果你有时间,你需要在小时、分钟和秒上变得更聪明一些。
BETWEEN should be pretty universal: how about something like this:
If you have times, you'd need to get a little smarter with the hours, minutes and seconds.
好的,发现有更好的方法(感谢 这篇文章):
基本上在模型中什么都不做!
Okay, found out there's a better way (thanks to this article):
Basically do nothing in the model!