Lets say I have the following table of Users
id, type
1, "A"
2, "B"
3, "B"
4, "A"
5, "B"
6, "A"
There are two user types. "A" users, and "B" users. "A"s can be connected to many "B"s, and "B"s can be connected to many "A"s. Let's say we have the following 'connections' table
id, A_id, B_id
1, 1, 2
2, 4, 2
3, 4, 3
4, 6, 5
5, 1, 5
6, 4, 5
Which would represent the following graph:
I could have an "A"s table that stores the foreign_key indexes of the users with the "A" type (i.e. 'these users are type "A"), and similarly for "B", in which case I could just define a simple has_many association through the connections table from "A" to "B" and vice versa.
I want to be able to type something like a.b to produce all the "B" that the 'a' is connected to, as well as b.a to produce all the "A" that the 'b' is connected to.
My question is: can this many-to-many association between "A" and "B" be defined using a single User model instead? Can a self-join be used?
Thank you for your time