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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:12:43+00:00 2026-05-20T00:12:43+00:00

I came across this strange issue at work so I created a minimal app

  • 0

I came across this strange issue at work so I created a minimal app to highlight the problem. I have two models with a simple association:

class Parent < ActiveRecord::Base
  has_many :children
end

and

class Child < ActiveRecord::Base
  belongs_to :parent
  named_scope :only_adults, :conditions => "adult is true"
end

Now when I do

 p = Parent.first
 p.children.only_adults.all()

I would expect rails to generate a single SQL query containing all the conditions. However, here’s what I see in the log:

Child Load (0.5ms)   SELECT * FROM "children" WHERE ("children".parent_id = 1) 
Child Load (0.3ms)   SELECT * FROM "children" WHERE ("children".parent_id = 1) AND ((adult is true) AND ("children".parent_id = 1)) 

The first query is basically useless and can be very time-comsuming in the case of large collections.

Does anyone have an idea why rails is behaving as such?

Note that instead of doing

p.children.only_adults.all()

I do

Child.by_parent(p.id).only_adults.all()

where by_parent is a named scope, then I get only one query.

Also note the duplication of the parent_id condition. This is not a big deal.

Thank you for your feedback.

  • 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-20T00:12:43+00:00Added an answer on May 20, 2026 at 12:12 am

    The reason has to do with the way queries are executed in Rails 2.3.x, as opposed to Rails 3.0.x. In Rails 2, calling p.children will automatically execute the query, regardless of the named scopes you have attached to the rest of the query. The only way to get around this is to use named_scopes in combination, like you do in “Child.by_parent(p.id).only_adults.all()” since the named scopes keep this behavior of postponing the query. In Rails 3 the query is built up until an execute keyword is found (count, all, first, last), so it’s possible to do the following in a single query:

    class Child < ActiveRecord::Base
      belongs_to :parent
      scope :only_adults, where(adult: true)
    end
    
    Parent.first.children.only_adults.all()
    
    # SELECT * FROM "children" WHERE ("children".parent_id = 1) AND (adult is true)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across a strange problem today. I created a subclass of UIView and
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I came across this question: say given two weights 1 and 3, u can
While working on a WebSocket server in Java I came across this strange bug.
While refactoring some code, I came across this strange compile error: The constructor call
Today i'm cutting video at work (yea me!), and I came across a strange
Today I came across a very strange JavaScript regular expression issue. When using the
Possible Duplicate: Strange floating-point behaviour in a Java program I came across this weird
I have been experimenting with GNU Radio and came across the tunnel.py program. This
While exploring for scjp questions, I came across this behaviour which I found strange.

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.