本文介绍了Rails:使该查询与数据库无关...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模型中有这两行是为PostgreSQL编写的:
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] }}
我正在生产环境中运行PostgreSQL,但是我正在使用SQLite3进行开发.我该如何以与数据库无关的方式编写这些行?
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?
顺便说一句,"recorded_on"是由以下内容组成的:
Btw, "recorded_on" is formed from the following:
Model.recorded_on = Time.parse("Fri, 01 May 2009 08:42:23 -0400")
推荐答案
好吧,发现有更好的方法(感谢这篇文章):
Okay, found out there's a better way (thanks to this article):
基本上在模型中什么都不做!
Basically do nothing in the model!
date = Date.new(year, month, 1)
Model.find(:all, :conditions => { :recorded_on => date..date.end_of_month })
这篇关于Rails:使该查询与数据库无关...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!