Should you not tell the models that they have many :team_members
for the :through
calls to work? Try adding:
has_many :team_members
to both the User
and Project
models.
EDIT: Looking at your setup more closely, it seems like the issue is that you're setting the counter cache on the :leader
relation, which has no corresponding inverse on the User
side. I'm not exactly sure which way you want it, but here is how I picture the options:
Option 1: Give the user a separate :lead_projects
association
class User < ActiveRecord::Base
has_many :lead_projects, :class_name => :Project, :inverse_of => :leader
class Project < ActiveRecord::Base
belongs_to :leader, :class_name => :User, :counter_cache => :lead_projects_count, :inverse_of => :lead_projects
and a migration adding :lead_projects_count
to the :users
table.
Option 2: Make the :projects_count
counter go through TeamMembers
As per this similar answer:
class User < ActiveRecord::Base
has_many :team_members
has_many :projects, :through => :team_members
class TeamMember < ActiveRecord::Base
belongs_to :user, :counter_cache => :projects_count
belongs_to :project
class Project < ActiveRecord::Base
has_many :team_members
has_many :users, :through => :team_members
With this option, I think your current migration is sufficient.
Note: I'm just writing code as I go here; please comment if I got the syntax wrong so I can edit my answer for future readers.