1

私は 1 つのモデル PunchingRequest と 1 つのモデル PunchingInformation を持っています。パンチインタイムまたはパンチアウトタイムの少なくとも 1 つに値が含まれている場合にのみ、パンチ要求テーブルにレコードを挿入したいと考えています。PunchingRequest と PunchingInformation の両方に関連するフィールドを含むフォームがあります。この検証を課すにはどうすればよいですか?

4

2 に答える 2

1

次のようなカスタム検証を使用します。

validate :presence_of_punch_in_time_or_punch_out_time

def presence_of_punch_in_time_or_punch_out_time
  # Use PunchingInformation.where(...)
  # or this.your_object_relation_with_punching_information
  # to get the other model row.
  errors[:base] << "Wrong punching information" unless row.punch_in_time || row.punch_out_time
end
于 2012-11-02T08:37:27.207 に答える
0

次のような PunchingRequest モデルでカスタム検証を書くことができます

validate :validate_punching_information

def validate_punching_information
  errors[:base] << 'Either punch in or punch out should be present' if self.punching_information.punch_in_time.nil? && self.punching_information.punch_out_time.nil?
end

PunchingRequest と PunchingInformation は 1 対 1 で関連していると想定しています

于 2012-11-02T08:31:47.417 に答える