我有两个对象预订和优惠券
我想根据以下验证将优惠券与预订相关联
以下代码的问题是语法不正确,其次我不能使用符号样式语法(:)运算符来访问第二个对象的内部值
with_options if: :coupon?, unless: coupon.future_useable? do |booking|
validates :start_time_minutes, numericality: {
greater_than_equal_to: coupon.start_time_minutes }
validates :finish_time_minutes, numericality: {
less_than_equal_to: coupon.finish_time_minutes }
end
编写上述代码的最佳方法是什么
答案 0 :(得分:0)
with_options if: :coupon?, unless: 'coupon.future_useable?' do
validates :start_time_minutes, numericality: {
greater_than_equal_to: -> { coupon.start_time_minutes} }
validates :finish_time_minutes, numericality: {
less_than_equal_to: -> { coupon.finish_time_minutes } }
end