简体   繁体   中英

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

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

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

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 }}}

Seems to be an issue with CanCan:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM