views:

85

answers:

1

I'm using the following 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?

+1  A: 

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

nas
Was thinking about doing this but I was hoping to be able to use the database time rather than web server time.
Alex
I don't think it should matter that much when you are already using ActiveRecord :) On top of that this will work across dbs
nas
I suppose it depends on the setup. It was my understanding that the created_at datetime was supplied from the database rather than the web server though. Is this not the case with ActiveRecord?
Alex