是的,所以我有一个用户表和不同的用户。我需要在更新之前验证用户表,然后才能进入主站点。
到目前为止,我刚刚完成了以下操作,是否有一种方法可以根据角色阻止验证,而无需执行自定义验证方法,例如with_options:on => :更新
before_validation:check_role
if check_role = "developer" do |dev|
dev.validate :first_name, presence: true # this doesn't work btw...
end
def check_role
return self.role_type unless self.role_type == nil
end
答案 0 :(得分:1)
我弄明白,这看起来是最好的方法:
class YourModel
with_options :if => lambda { |o| o.whatever == "whatever" } do |on_condition|
on_condition.validates_presence_of :address
on_condition.validates_presence_of :city
end
with_options :if => lambda { |o| o.condition_the_second == "whatever" } do |on_condition|
on_condition.validates_presence_of :foo
on_condition.validates_presence_of :bar
end
end
答案 1 :(得分:0)
with_options :if => Proc.new {|user| user.role_type == 'developer'} do |developer|
developer.validates :first_name, :presence => true
end
答案 2 :(得分:0)
validates :first_name, presence: true, if: :developer?
def developer?
role == 'developer'
end