I have an event table that contains start_at and ends_at. It also have a boolean for allday.
If the event is allday, then the user enters a field called hours. But, if they enter the start_at and ends_at, I’ld like to calculate and save in the hours field.
In English (not Ruby):
If event.allday = > true
hours = ends_at - starts_at
This way, I wouldn’t have to calculate it during all the different display pages I have and it would be easier to sum.
OR – would this logic go into the model?
Thanks!
UPDATE———–
I tried putting this into the model:
before_save :calculate_hours
def calculate_hours
if self.allday != true
self.hours = ((self.ends_at - self.starts_at)/ 3600).to_f.round(2)
end
end
Business logic always belongs in the model.