Skip to content

Instantly share code, notes, and snippets.

@codespectator
Created July 27, 2012 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codespectator/3186845 to your computer and use it in GitHub Desktop.
Save codespectator/3186845 to your computer and use it in GitHub Desktop.
Adding errors in a setter method
validate :full_name_length
def full_name
[forename, surname].compact.join(' ')
end
def full_name=(full_name_str)
name_arr = full_name_str.split(' ',2)
self.forename = name_arr.first
self.surname = name_arr.last
end
private
def full_name_length
errors.add(:full_name, 'is not correct') if full_name.split(' ',2).length != 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment