2015-02-05 65 views
0

我想用rufus调度程序调用我的Ruby on Rails模型,名为HelloWorld使用Rufus调度程序来调用我的Model类

但是下面用下面的错误而失败在我的控制台:

scheduler caught exception:

undefined method 'Foo' for #<Class:0x23371e0 ================================================================================ scheduler caught exception: undefined method 'Foo' for #<Class:0x23371e0> C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.21/lib/active_record/dynamic_matchers.rb:55:in 'method_missing' C:/my-dash/config/initializers/scheduler.rb:6:in 'block in <top (required)>' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in 'call' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in 'trigger_block' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in 'block in trigger' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in 'call' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in 'block in trigger_job'

我认为我没有正确地调用从计划我的模型?

内部文件'config\initializers\scheduler.rb'我有以下几点:

require 'rufus-scheduler' 
scheduler = Rufus::Scheduler.new 
scheduler.every '10s' do  
HelloWorld::Foo.new 
end 

我叫'app\models\helloworld.rb'HelloWorld Model类包含:

class HelloWorld < ActiveRecord::Base 
attr_accessible :my_name 

def Foo 
    my_var = "Some text here" 
    #and then do some more stuff here... 
end 
end 
+0

当您在调度程序之外调用HelloWorld :: Foo.new(例如,来自控制器)时会发生什么? – jmettraux 2015-02-05 21:54:17

+0

请包括错误的完整回溯,而不仅仅是第一行的开始。 – jmettraux 2015-02-05 22:17:03

+1

@jmettraux现在全错误更新:) – user2402135 2015-02-06 11:59:46

回答

0

HelloWorld.new.foo 

,而不是

尝试
HelloWorld::Foo.new 

你的问题与rufus-scheduler或Rails无关,只是你试图直接在类上调用实例方法。花时间学习Ruby。

你可以用这个程序,只是Ruby和你玩,没有Rails的,没有鲁弗斯调度:

class HelloWorld 
    def Foo 
    puts "Foo" 
    end 
end 

begin 
    HelloWorld::Foo 
rescue => x 
    p x 
end 

begin 
    HelloWorld::Foo.new 
rescue => x 
    p x 
end 

begin 
    HelloWorld.new.Foo 
rescue => x 
    p x 
end 

运行上面的产量计划:

#<NameError: uninitialized constant HelloWorld::Foo> 
#<NameError: uninitialized constant HelloWorld::Foo> 
Foo 

否则这个程序可能是更容易:

class Dog 
    def bark 
    puts "woa" 
    end 
end 

哪个代码不会产生异常:

Dog::bark.new 

rex = Dog.new 
rex.bark 

相关问题