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

Michael Mykytyn
Michael Mykytyn
23,440 Points

2nd Validation Quiz

Here is the question

Build a custom validator that ensures that no Contact’s last_name is Evil.

Here is my answer:

lass Contact < ActiveRecord::Base

validate :last_name_is_not_Evil

def last_name_is_not_Evil if @last_name.include?("Evil") errors.add(:last_name, "is Evil") end end

I get the ole Bummer Try Again message. Am I missing something really obvious?

Thanks!

4 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Michael,

the following should work:

 class Contact < ActiveRecord::Base
   validate :last_name_is_not_evil
 end

def last_name_is_not_evil
  if last_name.include?("Evil")
    errors.add(:last_name, "is Evil")
  end    
end
Hampton Catlin
STAFF
Hampton Catlin
Treehouse Guest Teacher

It was a simple error on capitalizing "Evil" in the method name. The way you had it would have worked technically, but typically in Ruby we don't use capitalizations in method names so the test was actually reacting to the style more than the substance.

Michael Mykytyn
Michael Mykytyn
23,440 Points

Ok thanks Hampton and thanks for the great class!