Reputation: 847
I have a two has_many
relationships present on 3 models.
Client has_many Projects
Project has_many Tasks
Client has_many Tasks through Projects
What I'm trying to do is find the 'nearest' start date, and 'nearest end date' of the tasks a client has applicable.
I have something similarly working to update the Project
when an earlier/later Task
is saved, but I seem to be having issues with updating the Client
.
The purpose of this is simpler querying for datatables, since I couldn't get filters to work on Client.project.tasks.start_date
def update_project_record
client = Client.find(project.client_id)
project.start_date = self.task_start_date if self.task_start_date < project.start_date
project.end_date = self.task_end_date if self.task_end_date > project.end_date
if client.nearest_project_start.present?
client.nearest_project_start = self.task_start_date if self.task_start_date < client.nearest_project_start
else
client.nearest_project_start = self.task_start_date
end
if client.furthest_project_end.present?
client.furthest_project_end = self.task_end_date if self.task_end_date > client.furthest_project_end
else
client.furthest_project_end = self.task_end_date
end
end
The project.start_date
and end.date
section work - I have made a field within client itself to house the 'earliest' and 'latest' task dates, but it currently is always printing out Nil.
I used the if/else
wrapper because I was getting a
ArgumentError: comparison of ActiveSupport::TimeWithZone with nil failed
response without it.
What's weird is if I byebug before that function ends, client
, project
, and task
are all showing their proper data, AND Client is showing the date it should. But if I use Rails Console, or the app itself, to save the record, Client is never getting updated.
This seems like a simple issue but I'm unsure how to figure out at which step it's having issues updating. I've tried using Console to perform the same query manually and sure enough it works perfectly fine.
Upvotes: 0
Views: 223