I have an app with Users and Jobs. Each User can have many Jobs. Each Job has a start_date and an end_date.
I want to create a filter that gets a list of users who have a min of X years of experience and a max of Y years of experience.
Years of Experience is typically calculated by getting the difference of start_date and end_date of each job of a user, however, there's a catch. If two jobs have overlapping dates, they should be calculated accordingly.
I created an attribute called work_experience that calculates the value of work experience based on the calculation above and appended it to the User model. So when I call /api/users, I have a 'work_experience' key with the correct value. All good until now.
But when filtering the users, I can't use the work_experience in query builder, since its calculated after the model has been retrieved. I would have to get list of all users with other filters, then filter the collection with required work_experience, which doesn't seem very optimized.
Is there a way to do it at query builder itself, without using the model attributes? Or should I just filter the collection even though it may be slower. I really suck at building queries, so I would like some help.