例如,如果我有一个模型:
User :firstname :lastname
joseph smith
anna bauer
... ...
还有一个输入字段,您可以在其中搜索用户。不同的搜索查询可能是:
searchquery1: smith joseph
searchquery2: joseph smith
searchquery3: joseph
searchquery4: smith
SQL 中的哪个搜索查询最好?其实我可以想象这个搜索查询:
where 'firstname OR lastname LIKE ?', "%#{search}"
第一次尝试:
def self.search(search)
if search
select('CONCAT(vorname, " ", nachname) as full_name, *')
where ['vorname LIKE :s OR nachname LIKE :s OR full_name LIKE :s', :s => "%#{search}"]
else
scoped
end
end
错误:SQLite3::SQLException: no such column: full_name
第二次尝试:
def self.search(search)
if search
a = search.split
where('firstname OR lastname LIKE ?', a[1])
where('firstname OR lastname LIKE ?', a[2]) unless a[2].nil?
else
scoped
end
end
问题:什么也没找到!