1

Rails 以某种方式添加AND (1=0)到模型的 SQL 查询中:

CompanyRelation Load (0.2ms)  SELECT `company_relations`.* FROM `company_relations` WHERE `company_relations`.`vendor_id` = 1 AND (1=0)
4

3 回答 3

2

如果您执行 where(params[:data]) 并且 data 它是具有空值的散列,您也可以在查询中找到 1=1 或 1=0 。例如你有 params hash live

{ village: { directions: { id: Empty Array }}}
于 2014-09-18T04:42:36.327 回答
0

似乎是CanCan的问题:

http://github.com/ryanb/cancan/issues/733

于 2013-11-11T01:03:07.550 回答
0

这是我发现的第一个问题,其中包括“rails scope 'AND 1=0'”。正如 rossmari 所提到的,可能有一个空的哈希值。您可能无意中将范围应用于用于生成哈希的条件。此范围可能会给您留下一个空的哈希值。例如

Assessment.last.children.for_user(user)

将在您可能不期望的 for_user 范围内应用子范围。在下面的示例中,id 的哈希值通过 id 参数传递。

如果你有类似的东西

scope :for_user, lambda { |user|
    where(id: Question.where(assigned_to: user).... #a hash

然后尝试添加无范围

scope :for_user, lambda { |user|
    where(id: Question.unscoped.where(assigned_to: user).... #a hash

编辑:我发现当我认为我需要无作用域时,很有可能我的查询结构不正确。

于 2019-08-24T15:00:42.393 回答