简体   繁体   English

With_Options执行不正确

[英]With_Options not executing properly

I have a module for validations and multiple types of products. 我有一个用于验证和多种产品类型的模块。 In this module I would like to store the conditions for the MOCAP product type. 在此模块中,我想存储MOCAP产品类型的条件。

These validations should run only if product_type_mocap? 仅当product_type_mocap吗? is true. 是真的。 However they run even if it is false... Does anyone have any ideas why? 但是,即使它们为假,它们也会运行...有人对为什么有任何想法吗? I can give further details as necessary 我可以根据需要提供更多详细信息

module Validations
  module Product
    module Mocap
      extend ActiveSupport::Concern

      included do
        with_options if: :product_type_mocap? do |product|
          product.validates :length,
          :frame_rate_type,
          :biped,
          :loopable,
          presence: true
        end

        def product_type_mocap?
          product_type_id == 5
        end
      end
    end
  end
end

Have you tried a different syntax? 您尝试过其他语法吗? Or possibly a lambda for the if ? 或者if可能的话是lambda? eg 例如

with_options presence: true, if: ->(obj) { obj.product_type_mocap? } do 
  validates :length, :frame_rate_type, :biped, :loopable
end

with_options will pass down the presence validator to each item and I find the lambda syntax works far more consistently than the symbol. with_options会将存在验证器传递给每个项目,我发现lambda语法的工作原理比符号更一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM