Forum: Ruby on Rails How would you model this? Thanks for any suggestions!

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
ms (Guest)
on 2008-10-30 03:49
(Received via mailing list)

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! :)

Best wishes,
faust45 (Guest)
on 2008-10-30 12:19
(Received via mailing list)
User can optional be a kind of contact person for one
or more towns

Please explain this more detail.
Thorsten M. (Guest)
on 2008-10-30 12:30
(Received via mailing list)
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
faust45 (Guest)
on 2008-10-30 13:05
(Received via mailing list)
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?
MaD (Guest)
on 2008-10-30 14:04
(Received via mailing list)
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 topic is locked and can not be replied to.