Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Active Record- State Machine NoMethodError

Hey purps!

I'm following the ActiveRecord Basics Deep Dive and am having major trouble with the State Change gem that was just added to the project.

I've successfully added the :state column, and all new entries get the :submitted state upon creation, but when I try to manipulate the :state in the Rails console, I'm met with this error:

NoMethodError: protected method `around_validation' called for #<StateMachine::Machine:0x00000101fb87c0>

I've Googled for a fix, and found a thread on the State Change's GitHub page, but it looks like people are just concerned that the gem might be forever broken due to lack of maintenance. Looks like the Deep Dive was made in March 2014, and that the comments State Change being on the verge of breaking show up around that same time. So maybe it'll have to be removed from the tutorial?? Not sure.

Regardless, if anyone has an idea about how to get this to work, I'M INTERESTED!

Per a suggestion on the afore mentioned thread, I've added the follow module to a config/initializers/state_machine_patch.rb file:

module StateMachine
  module Integrations
     module ActiveModel
        public :around_validation
     end
  end
end

But it still doesn't work. Not sure if I have to explicitly include this file somewhere for it to run, but as it stands, it's not helping.

Okay, thanks!

3 Answers

Oh Jeez, okay, so I edited this file: /Users/[USERNAME]/.rvm/gems/ruby-2.1.1@global/gems/activerecord-4.1.0/lib/active_record/callbacks.rb because that's where the error was occurring.

But in order to get the functionality to work I had to make a function public.......is this totally going to screw me?

This is the method that was private but is now public:

    def create_or_update #:nodoc:
      run_callbacks(:save) { super }
    end

I had to edit a file in a hidden directory and enter my password to save it. If anyone can tell me whether or not this is a bad idea, please do!

Thanks!

Anna, thanks for posting your experience and workaround! It helped me a lot in trying to find my own solution.

I had a similar problem, but I got the NoMethodError: protected method `around_validation' error on creation, new entries didn't even get assigned a state.

Anyhow, following the same GitHub thread that you referenced, I found a fork of the gem with a fix for this issue at: https://github.com/seuros/state_machine.

I then followed instructions here on how to specify a specific fork of a gem on github. In this case, you need to add the gem to the Gemfile as follows:

gem 'state_machine', :git => 'https://github.com/seuros/state_machine.git'

After running "bundle" from the command line and then restarting the server, I was back in business.

Cheers~

YES!!!!! Fantastic! Sungwon, thank you, dude, you're awesome.

Our Treehouse admin friends, this has gotta get fixed ASAP, because without Sungwon's fix, the normal state_machine gem breaks the whole app Hampton is demonstrating and describing in the video.

Thanks Sungwon, it works like a charm.

@Teamtreehouse, would be nice to add this fix to the teacher notes of the course.

Thanks sungwon choe I was stuck for a week before I found this thread!