【发布时间】:2019-08-21 12:20:24
【问题描述】:
我有一个这样的 ActiveRecord 范围:
scope :matching, ->(query) {
where Post.arel_table[:title].matches "%#{query}%"
}
这很好用。然后我将其更改为忽略title 中的额外空格:
scope :matching, ->(query) {
where "regexp_replace(title, '\\s+', ' ', 'g') ILIKE ?", "%#{query}%"
}
这可行,但我不得不从 matches()(在 Ruby 级别)降到 ILIKE(在 SQL 级别)。
regexp_replace(...) 可以和matches() 一起使用吗?例如:
scope :matching, ->(query) {
where handwaving("regexp_replace(title, '\\s+', ' ', 'g')").matches "%#{query}%"
}
(我尝试使用Arel::Nodes::NamedFunction,但无法使用。)
【问题讨论】:
标签: arel