Ruby strftime '%-m' not working in query

人盡茶涼 提交于 2019-12-12 01:22:20

问题


My goal is to query Items by their user_id, year created, and month created. I am using sqlite3 in development and postresql in production on Heroku.

I want to do something like this:

items.where("user_id = '?' AND strftime('%Y', created_at) = '?' AND strftime('%-m', created_at) = '?' ", 6, 2015, 3)

But it returns no records.

This works:

items.where("user_id = '?' AND strftime('%Y', created_at) = '?' ", 6, 2015)

But this returns no records:

items.where("user_id = '?' AND strftime('%-m', created_at) = '?' ", 6, 3)

Am I using the '%-m' format incorrectly?


回答1:


I would not use different databases in development and production, use the same one. Just configure postgresql on your localhost and change your query to something like:

user.items.where("date_part('year', created_at) = :year AND date_part('month', created_at) = :month", year: 2015, month: 6)

That's assuming that you have the associations setup between items and users, then you don't need to query on user_id as well.



来源:https://stackoverflow.com/questions/29739281/ruby-strftime-m-not-working-in-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!