Ruby on rails 为什么Rails会警告我一个不存在的SQL代码段?

Ruby on rails 为什么Rails会警告我一个不存在的SQL代码段?,ruby-on-rails,ruby,rails-activerecord,Ruby On Rails,Ruby,Rails Activerecord,我正在尝试从Rails 3.2到Rails 4的应用程序 我的代码中有一个特别的地方,就是发出这样的警告: DEPRECATION WARNING: It looks like you are eager loading table(s) (one of: users, one.example) that are referenced in a string SQL snippet. For example: Post.includes(:comments).where("comme

我正在尝试从Rails 3.2到Rails 4的应用程序

我的代码中有一个特别的地方,就是发出这样的警告:

DEPRECATION WARNING: It looks like you are eager loading table(s)
(one of: users, one.example) that are referenced in a string SQL
snippet. For example: 

    Post.includes(:comments).where("comments.title = 'foo'")

Currently, Active Record recognizes the table in the string, and
knows to JOIN the comments table to the query, rather than loading
comments in a separate query. However, doing this without writing
a full-blown SQL parser is inherently flawed. Since we don't want
to write an SQL parser, we are removing this functionality. From
now on, you must explicitly tell Active Record when you are
referencing a table from a string:

    Post.includes(:comments).where("comments.title = 'foo'")
        .references(:comments)

If you don't rely on implicit join references you can disable the
feature entirely by setting
`config.active_record.disable_implicit_join_references = true`.
(called from authenticate_common at
/path/to/project/app/models/user.rb:72)
但这行代码甚至不包含SQL:

def self.authenticate_common(login, password)
  return nil if login.blank? || password.blank?
  # this line outputs the warning:
  user = User.includes(INCLUDES_FOR_AUTH).where(:login => login).first
  # and so does this line
  user ||= User.includes(INCLUDES_FOR_AUTH).where(:email => login).first
  return nil unless user && user.authenticated?(password)
  user
end
这里的条件不仅没有使用SQL,甚至没有引用任何相关模型,因此无法执行建议的修复。此外,用户类中没有可以添加任何默认条件的作用域

_AUTH的INCLUDES_的值为:

但是,角色和权限类也缺少任何SQL代码段,因此不能是它们

我唯一剩下的想法是,Rails可能正在生成SQL代码段,然后在自己的检查中出错。。。这是怎么回事/

到目前为止的调查:

将includes设置为[]后,仍会显示警告。 从所有模型类的作用域中删除所有订单调用后,警告仍然出现。 如果我在整个代码中分散对references:user的调用,它会清除警告。我猜这只是掩盖了问题,因为我手头的问题是,我不确定为什么我必须首先调用引用,如果我必须调用它,我希望在导致需要它的代码块旁边这样做,而不是在所有地方。 它最终运行的这些查询对我来说没有多大意义,因为它们似乎没有返回实际记录:

从users.login='1'所在的用户中选择1someone@somewhere.com“限制1 从users.email='1'所在的用户中选择1someone@somewhere.com“限制1

从查询来看,似乎where子句是唯一一个使用我提供的数据的子句,但是那里的SQL肯定是Rails自己创建的


最终执行的是什么SQL?在用户、角色或权限模型中是否有任何默认作用域?输出警告的行是ActiveRecord,它被编译为SQL。它引用用户类。也许您应该将.references:user添加到活动记录查询中,看看这是否会停止警告。references:user确实会清除警告,但不清楚我为什么需要它,因为据我所知,整个应用程序中的大多数其他where调用都不会生成此警告。不过,还有一些是这样。就默认作用域而言,许多模型类都将->{order'name}作为默认作用域。很明显,很多这样的东西都会生成SQL,但是Rails开发人员关于不想编写SQL解析器的刺耳消息似乎是不正确的,因为我不是创建SQL的人。。。我不知道。它曾经出现在控制台上,但现在似乎不见了。我希望他们能在错误信息中包含这一点,事实上,这将有助于找出他们在哪里搞砸了/终于从日志中找到了SQL,但这不是很有启发性。这至少表明,没有订单和其他事情正在进行。这只是一个带有单个WHERE子句的基本查询,它显然是由Rails根据我传入的参数构造的。
INCLUDES_FOR_AUTH = [{ :roles => [ :permissions ]}]