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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:30:54+00:00 2026-06-14T00:30:54+00:00

in my User model I have a scope: scope :with_tournament_entrees, :include => :registers, :conditions

  • 0

in my User model I have a scope:

scope :with_tournament_entrees, :include => :registers, :conditions => "registers.id IS NOT NULL"

I wanted to see the SQL being generated by this scope and it produces:

"SELECT \"users\".* FROM \"users\"  WHERE (registers.id IS NOT NULL)"

I don’t see any mention of the include parameter I added and returns an error if executed in the console as raw SQL with the help of ActiveRecord::Base.connection.execute. If I was to query User.with_tournament_entrees in the console on the other hand it produces correct 8 records I need with no errors whatsoever, looking at the log I see the SQL statement being executed…

SELECT "users"."id" AS t0_r0, "users"."email" AS t0_r1, 
       "users"."encrypted_password" AS t0_r2, ....
       "registers"."id" AS t1_r0, "registers"."competition_id" AS t1_r1,
       "registers"."user_id" AS t1_r2 ... 
       FROM "users" LEFT OUTER JOIN "registers" ON "registers"."user_id" = "users"."id"
       WHERE (registers.id IS NOT NULL)

Which looks about right, so just to double check I copy the SQL and wrap it inside ActiveRecord::Base.connection.execute and it executes perfectly – except now instead of getting 8 records like I did when I called the scope I get 12 records.

What gives? It’s executing the exact same SQL right? So why am I getting different results? I’m using a PostgreSQL database.

Oh and I know you shouldn’t use scopes in Rails 3, which is why I’m trying to get at the SQL so I can figure out how to ActiveRecordify(?) it.

  • 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-14T00:30:55+00:00Added an answer on June 14, 2026 at 12:30 am

    This is explained by 2 facts:
    1. Adding joins effectively means you get a result set row per every valid combination of user row and a register row. So, if some user has 2 records in registers table – you get two rows for that user record.
    2. Active Record knows the above and is capable of “merging” rows, so that you get one user with two registers, not two same users with one register each.

    This way, running bare query yields the results from MySQL point of view, which is 12 rows. And ActiveRecord processes them down to 8 records, as some of the rows contain same user’s fields, just multiple registers’ ones.

    Hope this helps 🙂

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

Sidebar

Related Questions

I have a User model that has_many :posts. If I wanted to make a
I have the following named scope in my model class User scope :single_action, where(parent_id
Hi I have a named_scope in my User model as following. named_scope :by_gender, lamdba
I have a User model that has the following default_scope: default_scope where(account_id: Account.current_account.id) If
Let's say I have User model and Post model. Post model contains field user_id
I have a model that has a ForeignKey to the built-in user model in
Hello I have a user model and a ratings model. Whenever a new user
I have a simple User model which is associated to many Town objects using
I have one model User and will be creating another one Category . Both
I have defined a User model and added a featured field to it: class

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.