简体   繁体   中英

Ruby strftime '%-m' not working in query

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM