问题
I have a model called Community
and it has a column called name
I use this name
in sub-domain.
For example, when a user access to http://rockstar.test-sample.com
, it show the same content as http://test-sample.com/community/rockstar
obviously, this name
shouldn't be www
How can I prohibit www
if I'm stating that in models/community.rb
?
回答1:
You might want to spend some time with the 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
So something like this in your model should do the trick:
validates :name, :exclusion => { in: %w[www] }
来源:https://stackoverflow.com/questions/17668456/how-can-i-set-up-reserved-word-for-particular-column-in-validation