Isnor Creative
Isnor Creative Blog
Ruby, Ruby on Rails, Ember, Elm, Phoenix, Elixir, React, Vue

Apr 29, 2009

Ruby on Rails before_ callbacks must return true gotcha

Spent a heck of a long time trying to figure out why my Cucumber tests were failing this afternoon. I assumed it had to do with an API key problem (this test/model involved a spam checking service) until I discovered that “If a before_* callback returns false, all the later callbacks and the associated action are cancelled.” Cucumber was providing no feedback, and I was having no such problems when using the site myself. So I finally discovered, thanks to some other poor souls who went through this same hell before me, that tagging a return true on the end of the method will prevent this:

class Comment < ActiveRecord::Base
  before_create :is_it_spam?


  def is_it_spam?
    if RAILS_ENV == 'test' || Comment.disable_spam_check == true
      self.is_spam = false
    else
      self.is_spam = true if self.spam?  
    end                                  
    true 
  end  


end

I am available for Ruby on Rails consulting work – get in touch to learn more.

Gordon B. Isnor

Gordon B. Isnor writes about Ruby on Rails, Ember.js, Elm, Elixir, Phoenix, React, Vue and the web.
If you enjoyed this article, you may be interested in the occasional newsletter.

I am now available for project work. I have availability to build greenfield sites and applications, to maintain and update/upgrade existing applications, team augmentation. I offer website/web application assessment packages that can help with SEO/security/performance/accessibility and best practices. Let’s talk

comments powered by Disqus