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

Ruby

Code challenge: Adding States

I can't get passed this code challenge. In step 2 of 3, they say to "Call the "block_mutual_friendship" method after the transition to the blocked state."

This is the code you start with:

class UserFriendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'  

  state_machine :state, initial: :pending do
    state :requested
    state :blocked
  end
end

So, I added this:

after_transition on: :block, do: [:block_mutual_friendship!]

which makes the code look like this:

class UserFriendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'  

  state_machine :state, initial: :pending do
    after_transition on: :block, do: [:block_mutual_friendship!]
    state :requested
    state :blocked
  end
end

But that doesn't work. What am I doing wrong?

4 Answers

Hey Jon,

Thanks for the heads up. I'll notify Jason about this.

Thanks for being a Treehouse Student!

Ryan

Founder/CEO Treehouse

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hi Jon,

The code challenges don't always strictly repeat what's in the videos. Sorry for the confusion but I'm glad you figured it out!

I'm starting to learn that. :) Like I mentioned in another forum post, it could be nice for the users to make that clear from the beginning. We defnitely don't necessarily know that when we're new to the site. Thanks guys! Still enjoying myself.

Grrr. Apparently you have to remove the exclamation point, unlike what we saw in the video.

So adding

after_transition on: :block, do: [:block_mutual_friendship]

Or

after_transition on: :block, do: :block_mutual_friendship

will work.