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

Correct answer for custom validator?

I have the code of my answer attached.

I don't get any error messages from the built-in console, so don't know what's wrong.

contact.rb
 class Contact < ActiveRecord::Base
   validate :not_evil_last_name
   def not_evil_last_name
     if last_name.include?("Evil")
        errors.add(:last_name, "Name is evil!  Watch out!" )
      end
    end
   end
 end

2 Answers

Hi Erik,

Almost good, just missing you haven't add end after class Contact, and remove another two ends on the last part.

More easier to see below for readable better:

class Contact < ActiveRecord::Base
   validate :not_evil_last_name
  end

  def not_evil_last_name
     if last_name.include?("Evil")
        errors.add(:last_name, "Name is evil!  Watch out!" )
      end
  end
Jay Wilburn
Jay Wilburn
22,994 Points

You can't call a conditional on last_name without validating last_name.... You'll need to add this above your custom validator:

validates :last_name, presence: true

Should be fine then. :)