开发者

Avoiding use of 'requires' in my delayed_job classes in /lib folder

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 'F开发者_JS百科oo' without having to explicitly use 'requires'?


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_ */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜