Jim Jones
Jim Jones

Reputation: 1

Rails Devise users with multiple types - perhaps STI?

I have an application where there are users, which are managed by Devise. The application's actual users will add people as "friends" and one other type, lets call them "workers". The "friends" and "workers" are, or I would like them to be, all of type User, at the end of the day.

The catch is that there is a need for a User to be able to have a single person (User) in the system that is both a "friend" and a "worker" for them. There is also a need for non-duplication of Users by their unique id's, which in this case would be mobile phone number. If a given user adds another user as a "friend", and then subsequently adds them as a "worker", for example, it should simply be an association of some sort, and not a new User in the system.

I know how I would handle this in a traditional sense, but I'm sure there is some clever way with either STI or polymorphic associations to handle this in rails.

Any thoughts? It's worth noting that the various user types all have virtually identical fields (or will) associated with them - pretty straightforward id, name, mobile, etc.

I guess the trick is that any of the Users need to be able to have either or both of the "friend" or "worker" types associated to them, and each of those types should be able to have combinations of the other two associated with them.

Upvotes: 0

Views: 423

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84114

It sounds to me like the whether someone is a friend or a worker should be an attribute of the join model between the two people rather than an attribute of either person.

That way a person can be a friend of one person and another person's worker.

Upvotes: 1

Related Questions