ruby - 轨道模型 : validates_uniqueness_of doesn't remove trailing spaces not leading ones before unique check?

标签 ruby ruby-on-rails-3 model space validates-uniqueness-of

假设我对用户名实现 validates_uniqueness_of。如果名称“maddy”已经存在,那么它将接受值“maddy”作为唯一值而不是“maddy”。它应该删除两侧的空格。如何有这种行为?

最佳答案

class Person
  before_validation :strip_blanks

  protected

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

这段代码的来源包含一些关于为什么这不是默认的 Rails 行为的讨论。 http://www.ruby-forum.com/topic/166426

关于ruby - 轨道模型 : validates_uniqueness_of doesn't remove trailing spaces not leading ones before unique check?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5466036/

相关文章:

ruby - 将值与数组成员进行匹配/比较

ruby - 查找要在 pdf 文件中输出的区域

ruby-on-rails - 我如何对为什么 Ruby 在我的机器上突然变慢进行基准测试?

mysql - Rails 订单产品价格按最小值递增和递减 has_many 关系

asp.net-mvc - ASP.NET MVC 4 模型属性值从编辑 View 中丢失

codeigniter - $this->load->model() 在 CodeIgniter 中不起作用

ruby - 删除警报使用 watir webdriver 确认 javascript

ruby-on-rails - 时间不在 heroku 中更新

ruby-on-rails - 如何删除Rails 3中的迁移文件

tensorflow - ValueError : Error when checking input: expected dense_1_input to have shape (3, )但得到形状为(2,)的数组