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?…
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
Ainherited fromActiveRecord::Baseinstead of inheriting fromO).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
Oinstead ofA. This isn’t ideal since the scope may not be applicable to every subclass ofO, but it does seem to cause Rails to query the correct classes in thetypecolumn, and is a quick workaround.