I have three models in my program, in kind of a hierarchical structure:
User (has_many :computers)
Computer (has_many :programs, belongs_to :user)
Programs (belongs_to :computer)
Within the program, I have a need to see how many Programs a User has by extensions. This is pretty easy to do via User.computers.programs.
That said, would it be beneficial in any way to declare a has_many/belongs_to relationship between Users and Programs directly? Would there be any benefit (performance or otherwise), or would it just add complexity to the code?
It largely depends on whether you foresee requiring to access that relationship often. If you can do without that particular query, you’ll be better served to just use a
and be done with it. Less code that accomplishes the same is easier to read/maintain.
But if you will be making accessing that relationship over a large dataset it might payoff to denormalize your data a bit in the way you describe in the name of saving expensive
JOINs.