问题描述
我有一个名为 Community
的模型,它有一列名为 name
I have a model called Community
and it has a column called name
我在子域中使用这个name
.
例如,当用户访问http://rockstar.test-sample.com
时,显示的内容与http://test-sample.com/community/rockstar
For example, when a user access to http://rockstar.test-sample.com
, it show the same content as http://test-sample.com/community/rockstar
显然,这个name
不应该是www
如果我在 models/community.rb
中声明,我如何禁止 www
?
How can I prohibit www
if I'm stating that in models/community.rb
?
推荐答案
您可能想花一些时间使用 Active Record 验证指南:
You might want to spend some time with the Active Record Validations Guide:
2.4 排除
这个助手验证属性的值不包含在给定的集合中.事实上,这个集合可以是任何可枚举的对象.
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
所以在你的模型中这样的东西应该可以解决问题:
So something like this in your model should do the trick:
validates :name, :exclusion => { in: %w[www] }
这篇关于如何在验证中为特定列设置保留字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!