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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:48:56+00:00 2026-06-13T07:48:56+00:00

I implement a class hierarchy using STI pattern class A scope :aaa, where([someField]:[someValue]) end

  • 0

I implement a class hierarchy using STI pattern

class A
  scope :aaa, where([someField]:[someValue])
end

class B < A
end

The problem is that when I try to call something like:

B.limit(5).aaa
=> SELECT "[table]".* FROM "[table]" WHERE "[table]"."type" IN ('A') AND ([someField] = [someValue]) LIMIT 5

So I am getting 5 objects of type A, which satisfies scope :aaa
But I need to do the same with rows where type = “B”

Is there any way to use scopes from parent, without redifinning it in childs in STI pattern?

Thanks in advance

EDITED

I just discussed it with my frind and he showed me one important thing. A in not the root class of STI. IN fact whole hierarchy looks like

class O < ActiveRecord::Base
end

class A < O
  scope ..... .....
end

class B < A
end

maybe the reason is in hierarchy itself?…

  • 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-13T07:48:57+00:00Added an answer on June 13, 2026 at 7:48 am

    You are correct that this is a result of the multi-level hierarchy.

    The scope would work as expected if you had just one level of inheritance (i.e. if A inherited from ActiveRecord::Base instead of inheriting from O).

    STI in Rails does not always work as expected once you have intermediate classes.

    I’m not sure if this is a bug in Rails or just a result of it not being feasible to infer exactly what should be done. I’ll have to research that more.

    The scope issue you experienced is just one example of the unexpected behavior. Another example is that querying the intermediate class will only query for the class types it is aware of, which may not include all of its subclasses unless you specifically require them:

    See the following thread for more information, especially the post at the bottom by MatthewRudy: https://groups.google.com/forum/#!topic/hkror/iCg3kxXkxnA

    If you still want to use scopes instead of class methods as suggested by @Atastor, you can put the scope on O instead of A. This isn’t ideal since the scope may not be applicable to every subclass of O, but it does seem to cause Rails to query the correct classes in the type column, and is a quick workaround.

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

Sidebar

Related Questions

In a design of a class hierarchy, I'm using an abstract base class that
If I try to implement my class on this file I get an error
I'm trying to implement a class in C# with a method that intercepts the
Here I am trying to implement a class for complex numbers using books and
I'm currently trying to build a class hierarchy automatically by using C++ templates. The
I am trying to implement AHP(Analytic Hierarchy Process) algorithm for computing criterion's weights(using eigen
I need to create an object in one class hierarchy that wraps an object
I'm trying to implement class inheritance hieararchy with NHibernate and SQL server. I made
Consider this class hierarchy: Book extends Goods Book implements Taxable As we know, there
I want to implement a class which can be used by two classes of

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.