所以,我想要完成的事情可能很简单,但我不能把我的头绕过去。我有订阅用户和普通用户。普通用户只允许发布4次,而当前订阅的用户则允许发布10次。下面是我现在拥有的代码,它工作得很完美,但是验证只适用于所有类型的用户。
class Post < ApplicationRecord
validate :validates_posts!, on: :create
private
def validates_posts!
return if user.nil?
if user.posts.count > 4
errors.add(:base, 'Cannot add more than 4 Ads at a time')
end
end发布于 2018-10-02 04:51:46
你可以这样做:
if user.role == 'role1' && user.posts.count > 4
errors.add(:base, 'Cannot add more than 4 Ads at a time')
elsif user.type == 'role2' && user.post.count > 10
errors.add(:base, 'Cannot add more than 10 Ads at a time')
endhttps://stackoverflow.com/questions/52600668
复制相似问题