帮助轨活动记录查询(如从句) [英] Help with rails active record querying (like clause)
问题描述
我希望我的code做两件事情,目前没有做
@students = Student.where([如first_name = OR middle_name = OR姓氏=???,则params [:查询] .split])
-
工作。 (它说IM都要通过4个参数,但我希望用户能够通过这些字词在每个这些字段输入文字和查找并返回任何匹配)
-
其实使用类似条款,而不是刚性平等的条款。
请帮助。
这看起来像一个问题,那将是更适合于使用搜索,而不是SQL。您是否考虑过像思考狮身人面像或的充当鼬(Solr的很可能是矫枉过正)。
...如果你一定要在SQL中做到这一点,你可以建立一个查询是这样的:
COLS = ['如first_name','姓氏','middle_name']
查询=约翰·史密斯
sql_query = cols.map {| C | query.split.map {| Q | #{c}里像'?'}}。加入(OR)
sql_query_array = query.split * cols.count
Student.where(sql_query,sql_query_array)
I want my code to do two things that is currently not doing
@students = Student.where(["first_name = ? OR middle_name = ? OR last_name = ?", params[:query].split])
Work. (it says im supposed to pass 4 parameters but I want the user to be able to type words and find by those words on each of those fields and return whatever matches)
Actually use Like clause instead of rigid equal clause.
Please Help.
This looks like a problem that would be better suited to using search rather than SQL. Have you considered something like thinking sphinx or act as ferret (solr would probably be overkill).
...if you must do this in sql, you could build a query something like this:
cols = ['first_name', 'last_name', 'middle_name']
query = 'John Smith'
sql_query = cols.map{|c| query.split.map{|q| "#{c} like '?'"}}.join(' OR ')
sql_query_array = query.split * cols.count
Student.where(sql_query, sql_query_array)
这篇关于帮助轨活动记录查询(如从句)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!