我有这个搜索功能:
def self.search(query)
where('customer_phone_number LIKE ? OR lower(first_name) LIKE ? OR lower(last_name) LIKE ?', "%#{query.downcase}%", "%#{query.downcase}%", "%#{query.downcase}%")
end
这很棒,但如果有人在搜索栏中输入第一个和最后一个名字,则不会返回任何内容。如果用户同时输入名字和姓氏,如何检查全名呢?
答案 0 :(得分:1)
使用sql函数concat()
def self.search(query)
where("customer_phone_number LIKE ? OR lower(first_name) LIKE ? OR lower(last_name) LIKE ? OR concat(lower(first_name),' ', lower(last_name)) like ?", "%#{query.downcase}%", "%#{query.downcase}%", "%#{query.downcase}%", "%#{query.downcase}%")
end