English 中文(简体)
3 动态逃避查询错误的捆绑变量数
原标题:Rails 3 Dynamic Escaped Query wrong number of bind variables

我有一张表格,用户可以使用多种不同组合方式过滤。由于我从用户那里得到的所有输入数据,因此我必须从 SQL 中避开用户数据,这导致我现在面临的问题。我有两个基于发送到动作的参数动态构建的阵列,一个阵列包含SQL 条款,另一个阵列包含要与它匹配的数值。例如...

def results

  sql_clauses = Array.new
  sql_args = Array.new

  unless params[:elapsed_time].nil?
    sql_clauses << "elapsed_time = ?"
    sql_args << params[:elaped_time] 
  end
  unless params[:age_greater_than].nil?
    sql_clauses << "age > ?"
    sql_args << params[:age_greater_than]
  end
  .....
  @results = Model.where(sql_clauses.join(" and "), sql_args.join(", "))

end

将 sql_ clauses 阵列发送到方法没有问题的地方。 但是它在第二个参数上弹出, 因为它返回一个字符串, 并且它期待每个“ ” 字段的对应变量。 这些字段出现在 sql_ clauses 阵列中。 我尝试了 KandadaBoggu 在 < a href=> https:// stackoverflow.com/ questions/32551919/ combine- arrays- in- trains- in- rayss- in- raiders > > Comine 条件阵列 < / a > 。 这些选项对我都没有用, 但可能是因为我用了两个阵列而不是 1 。

有人知道我的问题有解决办法吗?

最佳回答

3. 主动记录方法,如选择、地点、顺序、限制等,返回主动记录:

cars = Car.where(:colour =>  black ) # No database queries are generated here.
rich_ppls_cars = cars.order( cars.price DESC ).limit(10) # Still no db queries.

当我们调用 .all , . first , . last , 或 .each 时,将会询问 db。

<强度 > Example Code

s 假设您正在用以下栏目查询模型:

  • name
  • elapsed_time
  • age

你有一个抛物线灰 看起来像这个:

{ :elapsed_time => 34, :age_greater_than => 14, :max_rows => 20 }

您的控制器动作可能看起来是这样 :

def results
    query = ModelName.select([:name, :elapsed_time, :age])
    query = query.where(:elapsed_time => params[:elapsed_time])  if params[:elapsed_time].present?
    query = query.where( age > ? , params[:age_greater_than])  if params[:age_greater_then].present?
    query = query.limit(params[:max_rows])  if params[:max_rows].present?
    @dynamic_query = query
end

注意,我在使用 .present? 测试参数键的存在。 这使我们无法将参数散列中的空白字符串错误为有效数据。

<强度 > 参考材料

问题回答

尝试一下这样的东西:

def results
  sql_clauses = []
  sql_clauses << "elapsed_time = :elapsed_time" if params[:elapsed_time]
  sql_clauses << "age > :age_greater_than" if params[:age_greater_than]
  .....
  @results = Model.where(sql_clauses.join(" and "), params)
end

"http://api.rubyonrails.org/classes/ActiveRecord/Base.html" rel="nofollow" > 其中 支持建筑条件的各种差异语法。

如果你有很多可能的文件需要包括的话 我会做一个地图字典,像这样:

map = {
   :elapsed_time => "elapsed_time =",
   :age_greater_than => "age >"
}

然后环形推动 参数 键,如果在 map 中存在的话,则构建条款。





相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签