If i load first coupon individual, gives me error saying coupon dosent
exists.
If i load first coupon, gives me error saying CouponIndividual dosent
exists.
But rails somehow know how to load everything.
Anyone can help me?
end
Anyone can help me?
You cannot use a class before it is defined. A simple solution
class Coupon
end
class CouponIndividual < Coupon
end
class Coupon
TYPES = [CouponIndividual]
end
More involved solutions use Class#inherited to track subclasses:
irb(main):001:0> class Base
irb(main):002:1> TYPES = []
irb(main):003:1> def self.inherited(chld)
irb(main):004:2> TYPES << chld
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> Base::TYPES
=> []
irb(main):008:0> class Inh < Base
irb(main):009:1> end
=> nil
irb(main):010:0> Base::TYPES
=> [Inh]
However, this only looks one level deep. For recursive handling you
need to ensure that child classes also override #inherited so they
register with Base (Coupon in your case).