Rails adds `AND (1=0)` to queries

不打扰是莪最后的温柔 提交于 2020-01-16 00:46:34

问题


Rails somehow adds AND (1=0) to the SQL query of a model:

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

回答1:


Also you can find 1=1 or 1=0 in your query if you do where(params[:data]) and data it's a hash with empty values. For example you have params hash live

{ village: { directions: { id: Empty Array }}}



回答2:


Seems to be an issue with CanCan:

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




回答3:


This is the first question I found that included "rails scope 'AND 1=0'". As mentioned by rossmari, there may be an empty hash. You may unintentionally be applying a scope to the conditions you use to generate a hash. This scope could leave you with an empty hash. For example

Assessment.last.children.for_user(user)

would apply the children scope inside the for_user scope which you might not be expecting. In the example below, a hash of ids is passed through the id parameter.

If you have something like

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

then try adding unscoped

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

Edit: I've found that when I think I've needed unscoped, there's a good chance I've structured my queries incorrectly.



来源:https://stackoverflow.com/questions/19897107/rails-adds-and-1-0-to-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!