EDIT : I'm using Mongoid, so no chaining nor has_many through:
I am developing a backend for project administration, and I have "deeply" related models
class Project
include Mongoid::Document
has_many :steps
scope :active, ...
class Step
has_many :assignments
class Assignment
belongs_to :contractor, inverse_of: :project_assignment
belongs_to :step
class Contractor
belongs_to :user, inverse_of: :contractor_profile
has_many :project_assignments, class_name: 'Assignment'
class User
has_one :contractor_profile, class_name: 'Contractor'
Now, I have coded a sidebar for my users. In this sidebar, If the User has a contractor profile, I want to display the number of active projects
What would be the best way to implement an access to this information ?
We're talking about maybe up to 30 active projects at a time, from 1 to 6 assignments per project (but many steps with same contractors), and contractors usually have a few projects total (active or not).
My ideas :
- Look at the
current_contractor
assignments -> steps -> projects -> count active ones - Look at active projects -> assignments -> Find contractors which match
current_contractor
- Maybe add a
belong_to :project
(how can I ensure it's the same asself.step.project
?) in every Assignment to be able to jump Contractor -> assignments -> projects & count