I have a report model which has many tools. Each tool has a working_hours attribute. The working hours attribute represents the total hours the tool has been used - you know, to keep track of servicing times.
The idea of the validation in question is to make sure that the tool is not used for more than 24 hours in a day. I have tried the following code
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
I get an error in the #{record.report.tool_name}
aspect of the code because the report_id
for the record is nil.
In the controller, I have @report.build_toolbox
which - I think - should build the association; thereby setting the report_id
How do I go about fixing this? Thanks