【发布时间】:2013-10-23 14:32:49
【问题描述】:
我有一个Listing 的模型belongs_to :user。或者,Userhas_many :listings。每个列表都有一个分类字段(dogs、cats 等)。 User 也有一个名为 is_premium 的布尔字段。
这是我验证类别的方式...
validates_format_of :category,
:with => /(dogs|cats|birds|tigers|lions|rhinos)/,
:message => 'is incorrect'
假设我只想让高级用户能够添加 tigers、lions 和 rhinos。我该怎么办?最好是用before_save 方法吗?
before_save :premium_check
def premium_check
# Some type of logic here to see if category is tiger, lion, or rhino.
# If it is, then check if the user is premium. If it's not, it doesn't matter.
# If user isn't premium then add an error message.
end
提前致谢!
【问题讨论】:
-
不,它不会工作。我想您应该改用自定义验证器。
标签: ruby-on-rails ruby ruby-on-rails-3 validation