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 ActiveRecord Basics Validation Errors 2

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

Build a custom validator that ensures that no Contactโ€™s last_name is Evil. issue with challenge

hey all im jacked up on a code challenge here and ive tried a few variations with no avail.. this is my code so far..

class Contact < ActiveRecord::Base
   validates :last_name, presence: true
                         uniqueness: true

   validate :name_has_no_evil

   def name_has_no_evil
     if @last_name.include?("Evil")
       errors.add(:last_name, "Can not be Evil")
     end
   end
 end

im not sure what else to try im sure this is something simple that im overlooking bur i've watched the video a few times and still cant pick up on it..

thanks in advance.

2 Answers

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

well i removed the @ but to no avail and the error treehouse send out is not helpful at all... just says bummer try again..

The validates :last_name, presence: true, uniqueness: true would be useful in a real app, but seems to screw up this code challenge. Try it without that.

I agree the error messages isn't very helpful. Thanks for pointing that out. We'll look into improving that.

In an ActiveRecord class you don't put @ in front of attributes like last_name.

Yes, you have to watch carefully to ignore the various permutations of incorrect answers the instructor goes through before arriving at a working example. (Initially, the instructor uses @ in the custom validator.) Unfortunately these are not edited out of the videos.