1
我要检查的唯一记录这个纪录created_at
之前只有属性为3个月,类似记录的属性值:使用的唯一性验证条件
validates :number, uniqueness: { conditions: -> { where('created_at > ?', Time.now - 3.months)}}
但不是Time.now
我想使用验证记录值为created_at
。
我该怎么做?
我要检查的唯一记录这个纪录created_at
之前只有属性为3个月,类似记录的属性值:使用的唯一性验证条件
validates :number, uniqueness: { conditions: -> { where('created_at > ?', Time.now - 3.months)}}
但不是Time.now
我想使用验证记录值为created_at
。
我该怎么做?
你写的东西应该已经有效了,如果你需要相反的话。
虽然可以重构一下,但是可以创建一个scope
来定义3个月内创建的所有项目。
scope :created_within_three_month, -> { where('created_at <= ?', 3.months.ago) }
然后你validates
写得很好。
validates :number, uniqueness: true, conditions: :created_within_three_month
注意:但是如果我是你,我会创建您模型state
,然后写穿上archived
超过3个月以上的所有记录rake任务。
然后我会在验证:
validates :number, uniqueness: true, conditions: -> { where.not(state: 'archived') }