简体   繁体   中英

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.

These validations should run only if 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 ? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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