views:

26

answers:

1

I have a class in my /lib folder that my delayed_job daemon calls to work on an object of type Foo. If I don't have a "requires 'foo'" in the worker class then it doesn't know what to do with the YAML it gets from the DB and I get the "undefined method" error. Adding in "requires 'foo'" will obviously fix this, which is the usual solution.

But..the problem is that I have counter_cache=>true in class Foo. So what happens is that when the DJ daemon loads the Rails environment, Foo gets auto-loaded and a callback is added for my counter_cache. Then the delayed_job daemon runs, performs the "requires 'foo'" and it obediently reloads Foo and adds yet another callback for the counter_cache, which results in my counter_cache getting updated twice for every row added or deleted.

Is there a way to wrap :counter_cache=>true so it only runs once? Is there a way to get my DJ worker class to use an object of type 'Foo' without having to explicitly use 'requires'?

A: 

unless defined?(Foo) could help:

class Foo
  ..
end unless defined?(Foo)

It reminds me C pattern of enclosing contents of my.h into #ifndef .. #endif:

#ifndef _MY_H_
#define _MY_H_

#endif /* _MY_H_ */