Rails nested with_option :if used in validation

后端 未结 1 930
醉酒成梦
醉酒成梦 2021-02-07 15:11
validate :updatable?  # First validation there is
with_options :if => Proc.new { |object| object.errors.empty? } do |updatable|
    updatable.with_options :if => \         


        
1条回答
  •  醉梦人生
    2021-02-07 15:27

    You are right that when nesting two :if conditions, the inner one will replace the outer one and always be checked. A workaround to handle two levels of nesting is:

    with_options :unless => !(outer condition) do
      with_options :if => (inner condition) do
    

    The if and unless conditions do not overwrite each other. I'm not sure I would call this a bug, but it would be nice if you could nest multiple :if conditions.

    0 讨论(0)
提交回复
热议问题