我有一个Listing
模型belongs_to :user
。或者,User
has_many :listings
。每个列表都有一个分类字段(狗、猫等)。User
也有一个名为 的布尔字段is_premium
。
这是我验证类别的方式...
validates_format_of :category,
:with => /(dogs|cats|birds|tigers|lions|rhinos)/,
:message => 'is incorrect'
假设我只想让高级用户能够添加老虎、狮子和犀牛。我该怎么办?最好用一种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
提前致谢!