How can I set up reserved word for particular column in validation?

核能气质少年 提交于 2020-12-12 10:22:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!