本文介绍了ActiveRecord::Relation#bind 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是出于好奇 - 我正在阅读 Relation::QueryMethods 的文档模块并发现该方法:

Just out of curiosity - I was reading the docs of the Relation::QueryMethods module and found that method:

def bind(value)
  relation = clone
  relation.bind_values += [value]
  relation
end

有人知道这是什么吗?我试图自己找,但失败了.

Does anyone know what is this? I tried to find by myself, but failed.

更新

我追踪了 @bind_values 的使用情况到 ActiveRecord::ConnectionAdapters 的无底深度——这些值会一直传递下去,直到低级 SQL 语句执行.似乎各个适配器可能会使用这些.我的猜测是它与像 SELECT * FROM 'table' WHERE 'field' = ? 这样的准备好的语句有关,但我被困在这里.任何人?

I tracked down usage of @bind_values to the bottomless depth of ActiveRecord::ConnectionAdapters - the values get passed on and on till low-level SQL statement executions. Seems that the individual adapters may use these. My guess is that it has to do with prepared statements like SELECT * FROM 'table' WHERE 'field' = ?, but I'm stuck here. Anyone?

推荐答案

首先,我想解释一下 find_by_sql 方法.看起来这个方法可以这样使用:

First, I would like to explain the find_by_sql method provided by ActiveRecord. It looks like this method can used like this:

Post.find_by_sql("SELECT title FROM posts WHERE author_id = ?", [author_id])

第二个参数称为binds",它是与查询中的问号对应的变量数组.您确实想使用 binds 数组将参数插入到您的查询中,因为它避免了很多 SQL 注入 如果你自己绑定会发生危险:

The second parameter is called "binds" and it is an array of variables that correspond to the question marks in the query. You really want to use the binds array to insert parameters into your query, because it avoids a lot of SQL injection dangers that happen if you did the binding yourself:

Post.find_by_sql("SELECT title FROM posts WHERE author_id = #{author_id}")

那么,这与 ActiveRecord::Relation 有何关系?AREL 的重点是您可以通过调用 ActiveRecord::Relation 对象上的方法一次一点点地构建查询.有很多这样的方法,以下是它们的一些列表:

So, how does this relate to an ActiveRecord::Relation? The point of AREL is that you can build up a query a little bit at a time by calling methods on an ActiveRecord::Relation object. There are a bunch of these methods, and here are some lists of them:

http://apidock.com/rails/v3.2.8/ActiveRecord/QueryMethods

所以 bind 方法通过克隆当前对象来创建一个新对象,将指定的 value 添加到 bind_values 列表中,然后返回新对象.最终,当该关系用于生成查询时,该值将发现自己被用于进行查询.一个 示例,其中 bind_value 传递给 find_by_sql 是在 exec_queries 方法中:

So the bind method makes a new object by cloning the current one, adds the specified value to the list of bind_values, and then returns the new object. Eventually, when the relation is used to generate a query, that value will find itself being used to make a query. One example where bind_values get passed to find_by_sql is in the exec_queries method:

@records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel, bind_values)

您可以在 activerecord gem 中搜索bind_values",您会发现几个类似的地方正在使用它.

You can search for "bind_values" in the activerecord gem and you will find several similar places where it is being used.

我原以为 bind 方法会被 where 调用,但它似乎没有在 activerecord 的任何地方被调用.也许它是旧设计的遗留物.我认为您不应该在您的应用中调用 bind.

I would have thought that the bind method would be called by where, but it doesn't seem to be called anywhere in activerecord. Maybe it is a left-over from an older design. I don't think you should call bind in your app.

这篇关于ActiveRecord::Relation#bind 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:57