Rails model: validates_uniqueness_of doesn't remove trailing spaces not leading ones before unique check?

StackOverflow https://stackoverflow.com/questions/5466036

سؤال

Suppose i implement validates_uniqueness_of on name of user. If name 'maddy' already exists then it will accept value ' maddy' as unique value but not 'maddy '. It should remove spaces both sides. How to have that behaviour?

هل كانت مفيدة؟

المحلول

class Person
  before_validation :strip_blanks

  protected

  def strip_blanks
    self.name = self.name.strip
  end
end

The source of this snippet contains some discussion of why this is not the default Rails behaviour. http://www.ruby-forum.com/topic/166426

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top