How would you model this? Thanks for any suggestions!


maybe you can help me. I am looking for a reasonable way to model the
following concept.

Assuming, we have 2 entities:

  1. User (table users)
  2. Town (table towns)

A user can, for example, add a town to the database, so we have 1:n
relationship (one user can add several towns) and the corresponding
foreign key in the town database, that’s allright. But now, let’s
suppose, that a user can optional be a kind of contact person for one
or more towns. What’s the right way to realize this?

I thought about an attributed join table between users and towns.
Since habtm doen not support attributes to be changed (as far as i
know), we could realize it through has_many :through…

But now, I ask myself if that’s the best way to model the relationship
between users and towns and I really asked myself, how to name this
join table. Normally, it’s common practice to name tables after nouns,
in this case maybe contact_persons. But I could it also name something
like “acts_as_contact_person_for” to get a bit more straight forward.
I really would like to have a good solution for this, because, I’ll
have a lot of such relations.

In conclusion: Do I generally have the right idea about that? How
would you model this? And what’s about the naming in this case? Is
there a kind of pattern for this problem?

I really thank you a lot! :slight_smile:

Best wishes,

User can optional be a kind of contact person for one
or more towns

Please explain this more detail.

Sounds good to me. Would do it the same way.
The intermediate table name is always a nasty decision to make.
I wouldn’t use the acts_as variant, since that’s used by plugins and
could be confusing.
I would go for either “users_towns” or “user_town_contacts”
Most likely for the user_town_contacts variant, if there is any
chance, that users and towns will in future get more kinds of

I understand!
Looking this:

User has many contacts
Contacts has field town
Then to find user contacts in town write User.contacts.find(:town =>
Why not?

you could model users polymorphic, such that users are either
operators or contacts.
then you would be able to say that a town belongs to a operator and
has many contacts.

hope this helps

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs