I have a resources
table set up in the following way:
resources
id - integer, pk
name - string
description - string
type_id - integer, fk
...
My goal is to set up a many-to-many relationship between the items in the same table. So when a user is viewing one resource they will also be able to see all the other resources that are linked to it.
I'm assuming I will need to set up a pivot table, something like ...
resource_links
id - integer, pk
id_left- integer
id_right - integer
Then I can set up a query scope to select entries from the pivot table where id_left or id_right is equal to the resource's id, and return all rows from the resources table where id_left/right is not equal to the current resource's id.
What is the best way to do what I want?
UPDATE:
My resources table also has a column to define what type of resource it is. Is it possible, using the belongsToMany
relation, to retrieve only those linked resources that are of a specific type?