【发布时间】:2014-05-01 01:39:59
【问题描述】:
我有一个包含电影列表的数据库。一个典型的条目如下所示:
id: 1,
title: "Manhatten and the Murderer",
year: 1928,
synopsis: 'some text...'
rating: 67,
genre_id, etc. etc.
现在我正在尝试通过一系列搜索测试,到目前为止,我已经通过了一个测试用例,如果您在文本字段中输入标题“Manhatten and the Murderer”,它会找到您想要的电影想。问题在于部分匹配。
现在我想要一种方法来搜索“Manhat”并匹配记录“Manhatten and the Murderer”。我还希望它与任何包含“Manhat”的电影相匹配。例如,它可能会返回 2 或 3 个其他类似标题:“我在曼哈顿的生活”、标题:“曼哈顿的大苹果”等。
下面是我目前在我的电影模型中的代码:
def self.search(query)
# Replace this with the appropriate ActiveRecord calls...
if query =~ where(title:)
#where(title: query)
binding.pry
end
end
我的问题是,我该如何设置?我的问题是“where(title:) 行。一个想法是使用正则表达式来匹配标题属性。任何帮助将不胜感激!谢谢。
【问题讨论】:
标签: ruby-on-rails regex database postgresql