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!

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

Could not get my custom validator to work on the second challenge

validate :evil_in_name

def evil_in_name
    if last_name.include?("evil")
        errors.add(:last_name, " is evil")

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

The challenge says "Evil", not "evil". You can either put this capitalized word in the parentheses after "include?" or chain a .downcase before the "Include?"

OMG, I can't believe that was it! Here I was racking my brains checking every letter of the actual ruby code, it didn't even occur to me to look at upper/lower case on the actual string. Thanks, boy I feel dumb.

This this challenge, another way is to use validates (with the "s") and the exclusion parameter. Here's the documentation that shows its usage: http://edgeguides.rubyonrails.org/active_record_validations.html#exclusion

Oh, and for motivated individuals, here's three more ways to do string validation :)