Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:16:40+00:00 2026-06-13T01:16:40+00:00

I have a table of around 100 Users and I also have an array

  • 0

I have a table of around 100 Users and I also have an array of user ids. What I wanted to do is show all users who are not a part of this array of user ids. When I do something like this

 User.where('id NOT IN (?)', [9, 2, 3, 4])

It successfully returns the records where the user’s id does not belong in that array. However if that array is empty like so

 User.where('id NOT IN (?)', [])

It does not return any users back and the SQL query looks like this

 SELECT "users".* FROM "users" WHERE (id NOT IN (NULL))

Does anyone know why this happens or could this be a bug? I am using Rails 3.2.5 with PostgreSQL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:16:42+00:00Added an answer on June 13, 2026 at 1:16 am

    ActiveRecord (3.2.1 at least) treats empty arrays as NULLs. The placeholders in a where call are handled by sanitize_sql. If you trace through the code for a bit, you’ll come to replace_bind_variables:

    def replace_bind_variables(statement, values) #:nodoc:
      raise_if_bind_arity_mismatch(statement, statement.count('?'), values.size)
      bound = values.dup
      c = connection
      statement.gsub('?') { quote_bound_value(bound.shift, c) }
    end
    

    and then quote_bound_value:

    def quote_bound_value(value, c = connection) #:nodoc:
      if value.respond_to?(:map) && !value.acts_like?(:string)
        if value.respond_to?(:empty?) && value.empty?
          c.quote(nil)
        else
          value.map { |v| c.quote(v) }.join(',')
        end
      else
        c.quote(value)
      end
    end
    

    An empty Array will satisfy all four conditions to get you to c.quote(nil) and that’s where your NULL comes from. All the special logic that leads to c.quote(nil) indicates that this is intentional behavior.

    Saying IN (or NOT IN) with an empty list:

    where c in ()
    

    should produce an SQL error so maybe the AR people are trying to prevent that by quietly turning that bad SQL into c in (null). Note that neither of these:

    select ... from t where c in (null);
    select ... from t where c not in (null);
    

    should ever produce any results due to the behavior of SQL’s NULL. This is a classic newbie mistake and the AR people really should know better.

    I’d prefer an exception myself: telling me that I’m about to deploy a foot-bullet would be much friendlier than just handing me a different gun.


    Executive summary:

    1. This “empty array means NULL” behavior is intentional.
    2. You should never ever try where('c in (?)', []) or where('c not in (?)', []) since neither statement makes much sense.
    3. Update your Ruby code to check for empty arrays and do whatever needs to be done to get the results you expect.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing around 100 columns, is it to possible to do
I have large table, with around 200 fields. Around a 100 of those fields
Say I have three tables, a table of users, a table of around 500
I have a large table with around 100 columns and hence is difficult to
I have a table with around 100,000 rows at the moment. I want to
I have this users table with: id : int (255) name: char (100) last_comment_target:
I have around 100 entries in MySql table, that contain a specific word in
I have table with around 70 000 rows. There is 6000 rows that i
I have a table with around 40 million rows, and I want to run
Just a small question regarding joins. I have a table with around 30 fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.