如何在验证中为特定列设置保留字?

MKK*_*MKK 2 validation ruby-on-rails ruby-on-rails-3

我有一个名为的模型Community,它有一个名为的列name

name在子域中使用它.

例如,当用户访问时http://rockstar.test-sample.com,它显示的内容与之相同http://test-sample.com/community/rockstar

显然,这name不应该www

www如果我说明的话,我怎么能禁止models/community.rb

mu *_*ort 6

您可能希望花一些时间使用Active Record Validations Guide:

2.4排除

此帮助程序验证属性的值不包含在给定集中.实际上,这个集合可以是任何可枚举的对象.

class Account < ActiveRecord::Base
  validates :subdomain, exclusion: { in: %w(www us ca jp),
    message: "Subdomain %{value} is reserved." }
end
Run Code Online (Sandbox Code Playgroud)

所以在你的模型中这样的东西应该做的伎俩:

validates :name, :exclusion => { in: %w[www] }
Run Code Online (Sandbox Code Playgroud)