Abstracting a date subtraction SQL query with Rails
I'm using the fo开发者_开发百科llowing as part of an ActiveRecord find method:
:conditions => "created_at > date_sub(now(), INTERVAL 7 DAY)"
This works properly on my development server where I'm using MySQL, but I'm deploying to postgreSQL which doesn't have a date_sub function. Ignoring the fact that I'm using two different databases, does Rails have anything built in to abstract this date subtraction operation?
If not, is there a recommended construct in Rails for specifying two different SQL queries that should be run in the event of encountering different DBs?
You could do
Object.find(:all, :conditions => ["created_at > ?", 7.days.ago])
#=> SELECT * FROM "objects" WHERE (created_at > '2010-02-03 12:06:19.352398')
should work across different dbs
精彩评论