rails3自定义验证重叠日期错误

时间:2014-02-27 20:58:54

标签: validation ruby-on-rails-3.2 date-range

构建一个必须检查属于同一(选项)父级的多个兄弟的验证器。

class Optionrate < ActiveRecord::Base
  belongs_to :option

  attr_accessible :from, :to, :option_id

  validates_presence_of :from, :to

  validate :not_overlap

  scope :overlaps, ->(from, to) do
    where "((from <= ?) and (to >= ?))", to, from
  end

  def overlaps?
    overlaps.exists?
  end

  def overlaps
    siblings.overlaps from, to
  end

  def not_overlap
    errors.add(:key, t('overlap_message')) if overlaps?
  end

  def siblings
    Optionrate.where('option_id = ?', option_id).all
  end

正在生成一个错误:“undefined方法`重叠'为[]:Array”引用语句

siblings.overlaps from, to

兄弟姐妹是复数这个事实让我觉得它期待一个数组,所以这很奇怪。

[另一个是where语句不接受 * where('option_id =?',params [:option_id])* 由于验证尚未完成尚未创建记录]

1 个答案:

答案 0 :(得分:1)

请在从.all移除Optionrate.where('option_id = ?', option_id).all后尝试运行代码,因为当您使用.Where时,无需使用.all方法。

或者

查看以下网址以供参考 http://guides.rubyonrails.org/3_2_release_notes.html#active-record