2

I use the following code to help find sales for the current 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")
2
  • 2
    Using sqlite for development and postgres for deployment is asking for trouble. Why not develop on postgres? Commented Aug 1, 2011 at 19:44
  • @Denis I am currently working on installing postgres to develop on. I just started on sqlite because I'm pretty new and didn't know that Heroku was using postgres. Commented Aug 1, 2011 at 19:51

3 Answers 3

4

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")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was a little unclear about how to use those functions.
1

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.

1 Comment

Yes. I plan to start running Postgres locally, although getting that up and running is causing trouble for me too :/
1

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")

1 Comment

Thanks. This works well too, but caused a different issue for me when I try to calculate the sum of each sales column and the sales variable is empty. I ended up going with the postgres functions and switching my dev db to postgres.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.