【发布时间】:2016-07-27 03:15:19
【问题描述】:
我正在尝试使LIKE 查询在我的开发中的 SQLite 和生产中的 PostgreSQL (Heroku) 中工作,而不对任一环境使用 if-else 语句。 if-else 语句似乎不是一个可行的选择。
我现在的回答是基于此处提供的解决方案:Generic Ruby solution for SQLite3 "LIKE" or PostgreSQL "ILIKE"?
这是我的课程模型的样子:
class Course < ApplicationRecord
has_many :enrollments
has_many :users, through: :enrollments
has_many :course_subjects
has_many :subjects, through: :course_subjects
def self.search(search)
where("lower(name) LIKE lower(?)", "%#{search}%")
end
end
如何构建我的 LIKE 查询以与 SQLite 和 PostgreSQL 兼容?
【问题讨论】:
标签: ruby-on-rails postgresql heroku sqlite pattern-matching