0

if shiftカスタムバリデーターをチェックインする必要がありますか?validates :shift, presence: true

どうにかしてリファクタリングできますか?

class ShiftLog < ActiveRecord::Base
  belongs_to :shift
  validates :shift, presence: true
  validate  :check_limit

  def check_limit
    if shift
      shift_logs = ShiftLog.by_shift(shift)
      if shift_logs.count >= self.shift.limit
        errors.add(:shift_id, "Exceeded limit")
      end
    end
  end
end
4

2 に答える 2

0

はい、再度確認する必要はありません。1 つのライナー条件を記述できることを除いて、問題ないように見えます。

 def check_limit
   shift_logs = ShiftLog.by_shift(shift)
   errors.add(:shift_id, "Exceeded limit")  if shift_logs.count >= self.shift.limit
 end
于 2013-09-17T13:50:11.847 に答える