nested has_many :through

here is the situation

class Entity < ActiveRecord::Base   belongs_to :parent, :foreign_key => :parent_id, :class_name => 'Entity' end

class Player < Entity end

class Team < Entity   has_many :players, :foreign_key => :parent_id, :class_name => 'Player' end

class Club < Entity   has_many :teams, :foreign_key => :parent_id, :class_name => 'Team'   has_many :players, :through => :teams, :foreign_key => :parent_id, :class_name => 'Player' end

Mysql::Error: Not unique table/alias: 'entities': SELECT count(*) AS count_all FROM `entities` INNER JOIN `entities` ON `entities`.parent_id = `entities`.id WHERE ((`entities`.parent_id = 48) AND (( (`entities`.`entity_type` = 'Team' ) ))) AND ( `entities`.`entity_type` = 'Player' )

how do get over this problem?

Do you really need the Entity class? What else does it do for you? I'd just have: Club has many Teams and Team has many Players.

STI is often not worth the pain it gives. You could also look into 'acts as tree' as a solution.

Hmmm... this is supposed to work - the join table should have its name aliased. This would be a good thing to add to the Rails Lighthouse - rails.lighthouseapp.com

--Matt Jones