相关对象相关属性的复杂轨道验证

时间:2016-03-23 07:49:25

标签: ruby-on-rails

我有两个对象预订和优惠券

我想根据以下验证将优惠券与预订相关联

以下代码的问题是语法不正确,其次我不能使用符号样式语法(:)运算符来访问第二个对象的内部值

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

编写上述代码的最佳方法是什么

1 个答案:

答案 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