我有一个名为 Community
的模型,它有一个名为 name
的列
我在子域中使用此名称
。
例如,当用户访问http://rockstar.test-sample.com
时,显示的内容与http://test-sample.com/community相同/摇滚明星
显然,这个 name
不应该是 www
如果我在 models/community.rb
中声明 www
,我该如何禁止它?
最佳答案
您可能想花一些时间研究 Active Record Validations Guide :
2.4 exclusion
This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
class Account < ActiveRecord::Base validates :subdomain, exclusion: { in: %w(www us ca jp), message: "Subdomain %{value} is reserved." } end
所以在你的模型中这样的东西应该可以解决问题:
validates :name, :exclusion => { in: %w[www] }
关于ruby-on-rails - 如何在验证中为特定列设置保留字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17668456/