简体   繁体   English

在帖子数据库字段中搜索

[英]Searching in posts db fields

I created a search function that searches the posts columns in the database 我创建了一个搜索功能,用于搜索数据库中的职位列

  def self.search(query)
    where("text like ?", "%#{query}%") 
  end

So this searches post.text. 因此,这将搜索post.text。 However I want to search through post.text and post.title for the specific keyword given. 但是我想通过post.text和post.title搜索给定的特定关键字。

I tried something like this: 我尝试过这样的事情:

  def self.search(query)
    where("text like ?", "%#{query}%" OR "title like ?", "%#{query}%") 
  end

but it doesn't work. 但这不起作用。 Any clues? 有什么线索吗?

You have to construct it manually or use a 3rd party plugin. 您必须手动构建它或使用第三方插件。 but the below should work 但以下应该工作

def self.search(query)
  where("text like :query OR title like :query", :query => "%#{query}%")
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM