Named many-to-many relationships in DataMapper
When implementing the collaborators feature in Trunks, ran into a bit of a roadblock with how to describe the relationships between the repository and the users.
Trunks is built on Merb and DataMapper, and the implementation involved two models: a User and a Project. In Trunks, a repository is a Project because "repository" is a reserved attribute in DataMapper and I was running into issues early on, so I changed it to Project.
A project belongs to a user, and a user has many projects. A user can also be a collaborator on many projects, and a project can have many collaborators. So I already had the first relationship which was a simple 1-to-many relationship. The problem came about with adding collaborators. In a sense, it is a normal many-to-many, which you can do in DataMapper similar to how you would in ActiveRecord using a "has_many ..., :through => ..." relationship, but I was creating a relationship between models that had an existing and separate relationship.
I didn't want the user.projects collection to return the projects a user collaborated on, because it was intended to only be the user's own projects. And similarly, a project only has one owner, not many.
I essentially found I needed a "has many through" with named attributes. To do this, I had to actually create the proxy class that goes between the user and the project model, rather than let DataMapper create it automatically. That way, I can control the attribute names on the proxy class so that "users" and "projects" on the two models won't get munged.
The result looked like this:
Having a "collab_users" on the Collaboration class allowed me to have the project model to get an attribute name "collab_users" instead of trying to use "users". And on user, I could have "collab_projects" instead of mixing things up with the existing "projects" attribute.
When creating the relationships though, I had to be careful to spell out the actual parent and child fields rather than let them be automatic, otherwise it'll run into issues where it'll try to create 'collab_user_id' and 'collab_project_id' and inserts will fail because those columns aren't being set.
Take the relationship definition on the user model:
I am essentially saying create an attribute named 'collab_projects' through the 'collaborations' relationship. 'collab_projects' will be of type Project. The child key on Collaboration is 'user_id' and the primary key on User is 'id'. Here, the fields I specify are betwen User and Collaboration, not on Project.
Then on Collaboration, I have:
Here I tell it that the attribute on Collaboration will be collab_user, but the model it is linking to is User. Basically saying I belong to user, but don't create an attribute named 'user'. I set the child key, which is the column on Collaboration, to be 'user_id'. I want to do that else it will try to create it as 'collab_user_id'.
Confused enough? All boils down to having two separate relationships between two models and having control over what the attributes are named. After all this, I end up with these calls: