问题
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