Sql "where" 比较日期数组 - rails

Sql "where" to compare array of dates - rails

我有一个数据库 table events,它有一列 dates 存储这样的日期数组

eg: ["2017-05-19","2018-04-19","2005-04-19","2017-06-19","2015-04-9"]

假设我给定月份值,05。我应该比较 dates 列中每个事件的日期月份和所有事件的 return 列表,其中它的日期正好包含月份 05所以,如果 event1 和 event2 在日期数组中有第 5 个月,return event1 和 event2。

I tried this, got confused. where("dates @> ARRAY[?]::varchar[]", don't_know_what_to_give_here)

这是未经测试的,但您可以使用 postgres array_to_string 方法,然后对结果字符串使用 LIKE 运算符。

select *
from your_table_name
where array_to_string(dates, ',') LIKE '%-05-%'

与Rails

Model.where("array_to_string(dates, ',') LIKE '%-?-%'", month)
# just make sure month has a leading zero e.g. 01 instead of just 1