Using the scope defined in the parent model, inside it is a child element (STI template)

I am implementing a class hierarchy using the 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 get 5 objects of type A that satisfy the scope: aaa But I need to do the same with the strings, where type = "B"

Is there any way to use regions from the parent without redrawing it in children in the STI template?

Thanks in advance

EDITED

I just discussed this with my frind, and he showed me one important thing. A in non-root class STI. In fact, the whole hierarchy looks like

class O < ActiveRecord::Base
end

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

class B < A
end

maybe the reason is in the hierarchy itself? ...

+5
source share
3 answers

, .

, , (.. A ActiveRecord::Base O).

STI Rails , , .

, ​​Rails , , . .

, , - . , , , , :

. , MatthewRudy: https://groups.google.com/forum/#!topic/hkror/iCg3kxXkxnA

, @Atastor, O A. , O, , , Rails type .

+2

, . A

def self.aaa
  A.where([someField]:[someValue])
end 

B

def self.bbb
  self.aaa.where("type = ?","B")
end
+1

STI, , .

:

class A < ActiveRecord
blabla
end
class B < A
scope :dates, ->(start_date, end_date) { where(:date => start_date..end_date) }
end
class C < B
<call scope_dates>
end
class D < B
<call scope_dates>
end

C D.

So, if you have an STI in Rails, please put the areas in the base class, not intermediate classes

0
source

All Articles