Help translating Rails Sqlite query to postgresql
I use the following code to help find sales for the curr开发者_开发技巧ent month. My development db is SQLite, but I'm using Heroku for production, which uses PostgreSQL. So I am getting an strftime function doesn't exist error. How would I translate this, and is there a way to translate it so that it still works when querying my dev SQLite db as well?
sales = current_user.location.sales.find( :all,
:conditions => ["strftime('%Y', date) = '?'
AND strftime('%m', date) = ?",
Date.today.year,
Date.today.strftime('%m')],
:order => "date")
Postgres does not support strftime. You can find a list of supported date/time functions here.
sales = current_user.location.sales.find( :all,
:conditions => ["extract(year from date) = '?'
AND extract(month from date) = ?",
Date.today.year,
Date.today.strftime('%m')],
:order => "date")
I think to_timestamp or to_char are the analogous Postgres functions you want to take a look at. The docs are pretty straightforward; basically similar to strftime with different template patterns: Postgres Datatype Formatting Functions
Maybe more than you're interested in, but I would really recommend running Postgres locally if you're doing a lot of work with Heroku. It will save you quite a bit of pain down the road.
If you are trying to get the sales figures for the current month, this might be a more appropriate, database agnostic query.
current_user.location.sales.where(:date => (Time.now.beginning_of_month..Time.now)).order("date")
精彩评论