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

  • Home
  • SEARCH
  • 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 6678653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:16:06+00:00 2026-05-26T04:16:06+00:00

I have two scopes with right outer joins declared on my User model. Here

  • 0

I have two scopes with right outer joins declared on my User model. Here they are

scope :users_with_school, joins("right outer join profiles on profiles.user_id = users.id     right outer join profiles_schools on profiles.id = profiles_schools.profile_id").uniq
scope :full_profile,  joins("right outer join profiles on profiles.user_id = users.id right outer join profiles_schools on profiles.id = profiles_schools.profile_id where users.pic_file_name is not null and profiles.occupation is not null and birthday is not null and profiles.desc is not null and hometown is not null ").uniq

My problem is that when I reload my console and call User.all I see both of these scopes being executed (i.e. I see the SQL in the console window) but I don’t see any other scopes. It doesn’t seem to effect the result of the query however I’m still confused as to why they get executed in the first place.

Here is an example
ruby-1.9.2-p290 :023 > reload!
Reloading…

 => true 
ruby-1.9.2-p290 :024 > User.all.count
User Load (296.5ms)  SELECT "users".* FROM "users" right outer join profiles on     profiles.user_id = users.id right outer join profiles_schools on profiles.id = profiles_schools.profile_id
 User Load (7.4ms)  SELECT "users".* FROM "users" right outer join profiles on profiles.user_id = users.id right outer join profiles_schools on profiles.id = profiles_schools.profile_id where users.pic_file_name is not null and profiles.occupation is not null and birthday is not null and profiles.desc is not null and hometown is not null
 User Load (435.8ms)  SELECT "users".* FROM "users" 

=> xxxx Users

Then when I execute the query again without reloading , it just executes the User.all query

ruby-1.9.2-p290 :025 > User.all.count
User Load (606.7ms)  SELECT "users".* FROM "users" 
=> xxxx Users (the same as above)

So, my question is, why is this happening and why is it only happening to these two scopes (out of 30 or so), the only difference I can see is that they are right outer joins

Obviously I don’t want these scopes to run every time I load a User class, at present it seems to be just when I reload everything though. Any insight ?

(using postgres, rails 3.1.1 ruby 1.9.2)

  • 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-05-26T04:16:07+00:00Added an answer on May 26, 2026 at 4:16 am

    I believe you are seeing this because .uniq acts on and returns an array, and so your scope does not return a scope.

    Try using SQL “DISTINCT” in a .select() after the join.

    scope :users_with_school, joins("right outer join ...").select("DISTINCT users.id")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I have a UIViewTable and a UISearchBar with two scope buttons. The
I have a generic question about scope and encapsulation. Take two scenarios: Scenario 1:
I have User model and Group model. User belongs_to Group . Group has an
I have two named scopes... both which work separately, but when combined do not
We have two Entity Framework queries, one with Include one with standalone query. Here
Have two folders with approx. 150 java property files. In a shell script, how
I have two applications written in Java that communicate with each other using XML
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two elements: <input a> <input b onclick=...> When b is clicked, I

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.