Yozuvlar doirasidan tashqarida tekshiruvlar

Menda ko'plab vositalariga ega bo'lgan hisobot modeli bor. Har bir asbobning work_hours xususiyati mavjud. Ish vaqti ko'rsatkichi vositaning umumiy soatlarini anglatadi - bilasizmi, xizmat ko'rsatish vaqtlarini kuzatib borish.

Tasdiqlash g'oyasi, bu vositaning bir kunda 24 soatdan ortiq foydalanilmayotganligiga ishonch hosil qilishdir. Quyidagi kodni sinab ko'rdim

class DayHoursValidator < ActiveModel::EachValidator

  def validate_each record, key, value
    most_recent_report = Report.where('tool_name LIKE ?', "%#{record.report.tool_name}%").last
    record.errors[key] << "has done more than 24 hours in a day" if value - most_recent_report.toolbox[attrubute] > 24
  end
end

Yozuv uchun report_id nil bo'lgani uchun kodning # {record.report.tool_name} tomonidagi xatolik yuzaga keladi.

In the controller, I have @report.build_toolbox which - I think - should build the association; thereby setting the report_id

Buni tuzatish uchun qanday ketaman? Rahmat

1

Javob yo'q

0