1

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)
Tim Baas
  • 6,035
  • 5
  • 45
  • 72
  • You are either making use of a scope (default or otherwise), or have some strange plugin that is doing this. Can you show the code that builds up the logic for your query? – PinnyM Nov 11 '13 at 00:41
  • I've added the model's code to the question.. It's dead simple.. I use Squeel, removing it doesn't fix anything.. – Tim Baas Nov 11 '13 at 00:48
  • Could it be CanCan? I use it to autoload my resources..? – Tim Baas Nov 11 '13 at 00:54
  • You haven't posted what is triggering the query in question, not to mention how that triggering object is being built... – PinnyM Nov 11 '13 at 00:57
  • I guess I found it: https://github.com/ryanb/cancan/issues/733 – Tim Baas Nov 11 '13 at 00:57
  • I'm having a similar problem [here](http://stackoverflow.com/questions/21886242/why-is-rails-is-adding-or-1-0-to-queries-using-the-where-clause-hash-syntax-wi/21887717) but I'm _not_ using CanCan. – Aaron Feb 19 '14 at 21:44

3 Answers3

2

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 }}}
rossmari
  • 81
  • 5
0

Seems to be an issue with CanCan:

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

Tim Baas
  • 6,035
  • 5
  • 45
  • 72
0

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.