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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:50:36+00:00 2026-06-02T06:50:36+00:00

I have a model with a has_many relationship, and a scope to determine whether

  • 0

I have a model with a has_many relationship, and a scope to determine whether or not it has any children, such as:

  scope :with_nomination, :include => [:nomination], :conditions => "nominations.service_id IS NOT NULL"

Using this, I can do something like Service.with_nomination and receive a list of all services with nomination children.

The problem is that when I do something like Service.select("id, firstName, lastName").with_nomination ActiveRecord in essense does a SELECT * FROM services which is very bad and does not utilize the indexes I so painstakingly set up.

How can I either rephrase my query or modify my scopes to work with the .select() command?

  • 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-02T06:50:38+00:00Added an answer on June 2, 2026 at 6:50 am

    Turns out in the syntax I was using, a select is not possible, so it does a select * and any further selects are already overriden.

    I re-wrote the scopes like so:

    scope :no_nomination, joins("LEFT JOIN nominations ON nominations.service_id = services.id").where("nominations.service_id IS NULL")
    # important distinction here, the left join allows you to find those records without children
    
    scope :with_nomination, joins(:nomination).where("nominations.service_id IS NOT NULL")
    

    Using this syntax allows me to do something like Service.select(:id,:user,:otherfield).with_nomination

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

Sidebar

Related Questions

I have a store model that has many products with a has_many :through relationship.
I have model called User with a has_many relationship to UserFilter . UserFilter has
I have an model (User) which has a has_many relationship to another model (Resource).
I have Product model and it has many categories with a has_many :through association
I have a Person model that has a many-to-many relationship with an Email model
I have two models in a has_many relationship such that Log has_many Items. Rails
I have a blog model with has many model comments relationship. So my form
Suppose I have a model Person which has a one to many relationship with
I have a notes model, which has a many-to-many relationship with the users model.
I have a model Author with a has_many relationship to model Book. Books are

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.