Ruby strftime“%-m”在查询中无效

Ruby strftime '%-m' not working in query

我的目标是按 user_id、创建年份和创建月份查询项目。我在 Heroku 上的开发中使用 sqlite3,在生产中使用 postresql。

我想做这样的事情:

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

但是returns没有记录。

这个有效:

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

但是这个returns没有记录:

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

我使用的 '%-m' 格式不正确吗?

我不会在开发和生产中使用不同的数据库,使用同一个。只需在本地主机上配置 postgresql 并将查询更改为:

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

这是假设您在项目和用户之间设置了关联,那么您也不需要在 user_id 上查询。