如何使用RubyOnRails编写MySQL多行条件?

如何使用RubyOnRails编写MySQL多行条件?,mysql,ruby-on-rails,conditional-statements,quote,Mysql,Ruby On Rails,Conditional Statements,Quote,我想在rails中的MySQL请求中使用“?”字符进行搜索。 例如,经典的方法是: Model.where("name = ?", '#{@search}') 我的问题是关于长查询和多行条件。 如果要手动生成条件: where = "" where << " status = 1 " where << " AND MATCH (name) AGAINST (? IN BOOLEAN MODE), @search " if @search @records = Model

我想在rails中的MySQL请求中使用“?”字符进行搜索。 例如,经典的方法是:

Model.where("name = ?", '#{@search}')
我的问题是关于长查询和多行条件。 如果要手动生成条件:

where = ""
where << " status = 1 "
where << " AND MATCH (name) AGAINST (? IN BOOLEAN MODE), @search " if @search
@records = Model.where(where)
where=“”

where您对where的内容有点困惑:您将变量名放在字符串中,这是行不通的:“@search”在字符串中的字面意思是“@search”,而不是变量

的参数(其中
的参数)视为对象数组的最佳方式,您可以这样构建它。第一个对象是查询字符串(带有?符号),其他元素是?的值?符号,这些符号将被rails清理和翻译

乙二醇

您可以将其他内容传递到where,例如值的散列或单个字符串,但数组是最灵活的,尤其是在您的情况下

记住这一点,您可以这样做来构建一个动态创建的复杂查询:我经常使用这种模式,因为它非常灵活,也非常可读

condition_strings = []
condition_values = []

condition_strings << "status = ?"
condition_values << 1 #or some dynamic data

condition_strings << "MATCH (name) AGAINST (? IN BOOLEAN MODE)"
condition_values << @search

conditions = [condition_strings.join(" AND ")] + condition_values
# => ["status = ? AND MATCH (name) AGAINST (? IN BOOLEAN MODE)", 1, "foo"]

#now we can use the array as an argument to `.where`:
@records = Model.where(conditions)
条件_字符串=[]
条件_值=[]

条件_strings您对where的内容有点困惑:您将变量名放在字符串中,这是行不通的:“@search”在字符串中的字面意思是“@search”,而不是变量

的参数(其中
的参数)视为对象数组的最佳方式,您可以这样构建它。第一个对象是查询字符串(带有?符号),其他元素是?的值?符号,这些符号将被rails清理和翻译

乙二醇

您可以将其他内容传递到where,例如值的散列或单个字符串,但数组是最灵活的,尤其是在您的情况下

记住这一点,您可以这样做来构建一个动态创建的复杂查询:我经常使用这种模式,因为它非常灵活,也非常可读

condition_strings = []
condition_values = []

condition_strings << "status = ?"
condition_values << 1 #or some dynamic data

condition_strings << "MATCH (name) AGAINST (? IN BOOLEAN MODE)"
condition_values << @search

conditions = [condition_strings.join(" AND ")] + condition_values
# => ["status = ? AND MATCH (name) AGAINST (? IN BOOLEAN MODE)", 1, "foo"]

#now we can use the array as an argument to `.where`:
@records = Model.where(conditions)
条件_字符串=[]
条件_值=[]

条件_字符串可能有引号问题-问题在哪里?问题是,如果我搜索类似于“bouquet d'alexander”的东西,sql查询将生成一个错误。引号可能有问题-问题在哪里?问题是,如果我搜索类似于“bouquet d'alexander”的东西,sql查询会生成一个错误。我对“select”也有同样的问题:
Model.select('articles.*,MATCH(name)对(“++@search+”,在布尔模式下)作为相关性”)。其中(where)
正如您所见,我需要一个自定义的“select”,假设我不能使用?选择中的符号。使用Rails消毒的替代方法是什么?将其放在“.where”中,而不是放在“.select”中。但是,更一般地说,你应该对此提出自己的问题。我需要把它放在select中,因为我需要“作为相关性”字段来对结果进行分类。我将打开一个新问题。我有一个与“选择”完全相同的问题:
Model.select('articles.*,MATCH(name)对(“++@search+”,在布尔模式下)作为相关性”)。where(where)
如您所见,我需要一个自定义的“选择”,假设我不能使用?选择中的符号。使用Rails消毒的替代方法是什么?将其放在“.where”中,而不是放在“.select”中。但是,更一般地说,你应该对此提出自己的问题。我需要把它放在select中,因为我需要“作为相关性”字段来对结果进行分类。我将提出一个新问题。
condition_strings = []
condition_values = []

condition_strings << "status = ?"
condition_values << 1 #or some dynamic data

condition_strings << "MATCH (name) AGAINST (? IN BOOLEAN MODE)"
condition_values << @search

conditions = [condition_strings.join(" AND ")] + condition_values
# => ["status = ? AND MATCH (name) AGAINST (? IN BOOLEAN MODE)", 1, "foo"]

#now we can use the array as an argument to `.where`:
@records = Model.where(conditions)