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 8431791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:57:04+00:00 2026-06-10T05:57:04+00:00

I am running Rails 3.2.8 and ruby 1.9.3p194. I’m trying to better understand exactly

  • 0

I am running Rails 3.2.8 and ruby 1.9.3p194.

I’m trying to better understand exactly how the exists? method works in activerecord. I am getting some weird results that I don’t understand… I am going to simplify my code here for the sake of clarity.

Imagine I have a User model with a list of users.

1 Andy, m
2 Jake, m
3 Cathy, f
4 Mike, m

I have a scope for male, so:

User.male => [#< User id: 1, name: Andy, gender: male>, #< User id: 2, name: Jake, gender: male>, #< User id: 4, name: Mike, gender: male>]

OK. now:

a = User.find 1 => #<User id: 1, name: Andy, gender: male>  
b = User.find 2 => #<User id: 2, name: Jake, gender: male> 
c = User.find 3 => #<User id: 3, name: Cathy, gender: female> 
d = User.find 4 => #<User id: 4, name: Mike, gender: male> 

User.male.exists?(a) => true  
User.male.exists?(b) => true  
User.male.exists?(c) => false  
User.male.exists?(d) => true  

Ok that makes sense. However:

User.male.limit(1) => [#<User id: 1, name: Andy, gender: male>]

User.male.limit(1).exists?(a) => true  
User.male.limit(1).exists?(b) => true  
User.male.limit(1).exists?(c) => false  
User.male.limit(1).exists?(d) => true  

Why does exists?(b) and exists?(d) return true? Is it because the limit doesn’t actually change the query which is still all male users, and limit(1) is simply a display thing? I can buy this, but then what would the correct query be if I only wanted exists?(a) to return true, and everything else to return false?

Okay, now this really confuses me completely:

User.male.limit(1).offset(1) => [#<User id: 2, name: Jake, gender: male>]  

User.male.limit(1).offset(1).exists?(a) => false
User.male.limit(1).offset(1).exists?(b) => false
User.male.limit(1).offset(1).exists?(c) => false
User.male.limit(1).offset(1).exists?(d) => false

Why does everything here return false? I want exists?(b) to return true, and I can’t figure out at all why it doesn’t. If anyone can enlighten me, I would be very grateful. I looked through the docs to no avail.

  • 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-10T05:57:05+00:00Added an answer on June 10, 2026 at 5:57 am

    User.male adds a where clause “gender = male”

    .exists?(a) adds an extra clause “id = 1”

    limit 1 just adds a limit 1 after the where clause.

    So User.male.limit(1).exists?(a) will generate
    SELECT 1 FROMusersWHEREusers.id= 1 AND (gender =male) LIMIT 1

    So the reason why .exists?(x) works is because it adds the id to the where clause and thus selects the relevant record.

    Also the offset(1) wouldn’t work because you only get 1 matching record when you put the exists?(x) and adding an offset(1) to that would mean that it should look for records after that one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When running Rails 3 RC with Ruby 1.9.2.rc2 under RVM I keep getting a
I am trying to implement ruby on rails app using Capistrano, I am running
I was trying to get the latest ruby on rails for my Mac running
I am running Rails 3 and Ruby 1.8.7 on Windows. I am trying to
I am running Rails 3.2.7 with Ruby 1.9.3p194 to output JSON data from an
I am running a Rails (3.2.3) application with Ruby 1.9.3p194 on the basic Ubuntu
I'm running rails 3.2.1 with rspec 2.8.1 on ruby 1.9.3p0 and when running my
I have a ruby on rails app running a server and sometimes it needs
I have a Ruby on Rails app running on my server, and I can't
I am running a Ruby on Rails app on a virtual Linux server that

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.