2

I'm trying to create a cron job using the whenever gem to send birthday reminders to users and i want this cron to run everyday.

I have the whenever gem working, but my cron job keeps erroring out. I'm trying to call a controller method in the rails console to figure it out, but i keep getting errors and i'm unsure why.

I have this controller:

class BirthdayRemindersController < ApplicationController

    include ApplicationHelper

    # cron job that sends birthday reminders
    def send_birthday_email_reminders 
        users = User.all

        email_addresses = []

        users.each_with_index do |user, i|
            if user.user_details.birthday_reminders == true
                email_addresses[i] = get_primary_email(user)
            end
        end

        p email_addresses

        users.each do |user|
            if user.user_details.birthday == Date.today
                p "reminder sent"
                send_birthday_reminders(user, email_addresses)      
            end
        end
    end
end

And in the rails console i've tried both of these and they both error out.

Toms-Mac-mini:famnfo TomCaflisch$ rails c
Loading development environment (Rails 3.2.9)
irb(main):001:0> BirthdayRemindersController.send_birthday_email_reminders
NoMethodError: undefined method `send_birthday_email_reminders' for BirthdayRemindersController:Class
        from (irb):1
        from /Users/TomCaflisch/.rvm/gems/ruby-1.9.3-p327@famnfo/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
        from /Users/TomCaflisch/.rvm/gems/ruby-1.9.3-p327@famnfo/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
        from /Users/TomCaflisch/.rvm/gems/ruby-1.9.3-p327@famnfo/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):002:0> BirthdayReminders.send_birthday_email_reminders
NameError: uninitialized constant BirthdayReminders
        from (irb):2
        from /Users/TomCaflisch/.rvm/gems/ruby-1.9.3-p327@famnfo/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
        from /Users/TomCaflisch/.rvm/gems/ruby-1.9.3-p327@famnfo/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
        from /Users/TomCaflisch/.rvm/gems/ruby-1.9.3-p327@famnfo/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):003:0> 

What am i missing? I have no routes defined for this controller because i don't want anyone to be able to hit it via a web brows

3
  • It may help: stackoverflow.com/questions/151030/…
    – Baldrick
    Jan 5, 2013 at 15:09
  • I know how to call the controller from the console as i'm doing that. I was just getting an error when calling a specific method and wasn't sure why.
    – Catfish
    Jan 5, 2013 at 15:13
  • Yves Senn pointed it: you're calling the method as if it was a class method (declared with self), but it is an instance method. You can try to call it like that BirthdayRemindersController.new.send_birthday_email_reminders , but it will certainly generates an error because the Controller is missing context.
    – Baldrick
    Jan 5, 2013 at 15:18

2 Answers 2

5

The methods on your controller are instance methods, not class methods. Class methods are defined with self.. It is hard to trigger a controller action from the console because you miss the whole context. You don't have a session, no request, no response. All these things make it hard to work with code inside controller actions. If you need to trigger your code from outside (console, rack-task, other code). You should extract the code into it's own class.

2
  • Can you provide an example? What folder would this go into? Do i just not extend the ApplicationController?
    – Catfish
    Jan 5, 2013 at 14:15
  • It was clear from first reading your question that all i needed to do was add self. to the method. I figured it out now after reading some things on the internet.
    – Catfish
    Jan 5, 2013 at 15:33
0
  1. Use 'debugger' gem.
  2. Add a debugger in you controller method and then play with it in your console once you run the application in debugger mode using -> "rails s --debugger"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.