1. Whenever: A Hidden Gem

    After learning cron syntax, figuring out what the PATH is during cron execution and how to set it in my crontab, and documenting my crontab within my application, I discovered Whenever.  Javan Makhmali’s Whenever gem allows you to write self-documenting ruby in your application to define your cron jobs, then integrates beautifully with Capistrano to update  your crontab on deploy.  It also saves me from having to lookup cron syntax every few months when I need to edit something (and have, of course, forgotten the syntax).

    The documentation for Whenever is very good, and there’s even a Railscast that provides a step by step.  But there is one setting not mentioned anywhere, which I only found digging through the code:

    set :set_path_automatically, false

    Nominally, Whenever writes a PATH=... line before it lists your cron jobs.  This is fine most of the time, but on some systems (like Joyent’s SunOS 5.11) the extended Vixie Cron syntax (the standard used on most *nix systems) is not supported.  As such, you may see an error like

    crontab: error on previous line; unexpected character found in line.

    if PATH is set in your crontab.  So, disable it in your schedule.rb and get back to real work.

    p.s. Will Tumblr ever create a post type for code?

     

    tags:  ruby  error  gem  rails 

    Comments
  2. Rails InvalidAuthenticityToken

    I am a big fan of the Rails plugin exception_notification.  It’s dead simple to integrate with your app and it alerts you to any problems as they happen.  You get an email with all the information you need to resolve the issue.  So, after reading the email you 1) fix the bug 2) cap deploy 3) email the affected user and 4) profit (i.e. make your user happy)!

    For a few weeks now I’ve been getting emails with the subject ActionController::InvalidAuthenticityToken.  I wasn’t sure why, and couldn’t track it down since I couldn’t reproduce it.  When using restful_authentication you get two logout methods: logout_keeping_session! and logout_killing_session!. As it turns out, I was killing the session in a couple of controllers.  If the user then tries to log in, their authenticity token will no longer be valid, which produces the aforementioned InvalidAuthenticityToken error.  (The plugin expressly warns against using logout_killing_session!, by the way).

    Rails is powerful.  You can express a lot with little code, and the framework supports you from all over.  This results in a learning curve that isn’t steep, but is longer than most.  I learn something new about Rails (and Ruby) weekly, usually right after I break it.  That’s how I (like to) learn.

     

    tags:  ruby  rails  error  bug 

    Comments
  3. Cryptic ActiveRecord::Observer error

    Why do programming languages supply such awful and cryptic error messages?  From what I’ve read of Object-C 2.0, Apple is working to change all this.  But until I start coding in Obj-C 2.0 or Ruby gets in line, I’ll have to continue following my gut before following the error message…

    I added an observer to my environment.rb file like follows:

    config.active_record.observers = :listing

    Since I created the observer with the rails generator, I should have written:

    config.active_record.observers = :listing_observer

    Shouldn’t be a big deal, except I continued to code a few new things before testing it all out.  When I saw the following error message, it wasn’t immediately clear that I had written :listing where rails wanted :listing_observer:

    /.../lib/active_record/base.rb:1959:in `method_missing_without_paginate': undefined method `instance' for #<Class:0x103568ca0> (NoMethodError)

     

    tags:  ruby  rails  error  fail 

    Comments
  4. blog comments powered by Disqus