user2402135
user2402135

Reputation: 371

Use Rufus scheduler to call my Model class

I am trying to use rufus-scheduler to call my Ruby on Rails Model called HelloWorld.

However the below is failing with the following error in my console:

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'

I assume I am not calling my Model from the Scheduler correctly?

Inside file 'config\initializers\scheduler.rb' I have the following:

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

My Model class called HelloWorld in 'app\models\helloworld.rb' contains:

class HelloWorld < ActiveRecord::Base
attr_accessible :my_name

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

Upvotes: 0

Views: 738

Answers (1)

jmettraux
jmettraux

Reputation: 3551

Try with

HelloWorld.new.foo

instead of

HelloWorld::Foo.new

Your issue has nothing to do with rufus-scheduler or Rails, it's just you trying to call an instance method directly on the class. Take the time to learn Ruby.

You can play with this program, just Ruby and you, no Rails, no rufus-scheduler:

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

Running the program above yields:

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

Or else this program might be easier:

class Dog
  def bark
    puts "woa"
  end
end

Which code will yield no exception:

Dog::bark.new

or

rex = Dog.new
rex.bark

?

Upvotes: 1

Related Questions