Error loading files

I need to load all my models from rails inside a ruby program that gonna
process a queue.

The problem is i have this structure.

app/models/coupon.rb
app/models/coupons/coupon_individual.rb

class Coupon
TYPES = [CouponIndividual]
end

class CouponIndividual < Coupon
end

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?

On Tue, Nov 29, 2011 at 2:28 PM, Diego B.
[email protected] wrote:

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).

Kind regards

robert

Thanks, that solved my problem!
Didnt know such kind of callback exists.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs