I have this (from my limited perspective at least) somewhat
complicated data structure, and I’m having trouble wrapping my head
around the associations I need.
Here’s how it works, I’m trying to model a tree of skills. For anyone
who’s ever played RPGs this should be familiar. But you can imagine
something like this:
Skill: Java Programmer: requires Programmer at level 1
Skill: Ruby P.mer: requires Programmer at level 3
Skill: Rails Programmer: requires Ruby P.mer at level 3 and
Programmer at level 4.
So the skill requirements are self-referential associations, but with
extra data. A skill can have any number of skill requirements.
This is what I envisioned:
Model: Skill, has_many RequiredSkill, belongs_to RequiredSkill (other
data: name, rank)
Model: RequiredSkill, belongs_to Skill, has_one Skill (other data:
The problem is that a Skill can belong to any number of
RequiredSkills, so the simple belongs_to association doesn’t work. On
the other hand, a has_and_belongs_to_many association can’t work,
because a RequiredSkill doesn’t belong_to any Skill that it has, nor
does a Skill belong_to any RequiredSkill that it has.
The solution may very well be a simple one, but I don’t have enough
experience with join tables to figure it out.