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

Having trouble with custom validation

class Contact < ActiveRecord::Base

   validate :last_name_not_evil

   def last_name_not_evil
     if last_name.include?("evil")
       errors.add(:name, "cannot be evil")
     end
   end
 end

Requirement is to build a custom validation that ensures last_name is not Evil.

I keep getting "bummer"'ed, though.

Anything I'm missing?

1 Answer

They want "Evil", not "evil" and they want last_name attribute, so:

     if last_name.include?("Evil")
       errors.add(:last_name, "cannot be evil")

Very, very tired. Thanks for the catch. :)

I actually pasted one of the futile frustrated attempts where I was changing things I didn't think mattered, like the case.

I had most things right most of the time, except I never changed the symbol to last_name.

I'll try sleeping sometime this week, see if I can avoid some of the sillier stuff,

Yeah, sleep might help - you have to pay attention to every space (literally) when coding :)