# How would you model this? Thanks for any suggestions!

**URL:** <https://www.ruby-forum.com/t/how-would-you-model-this-thanks-for-any-suggestions/152091>\
**Category:** Rails\
**Created:** [October 30, 2008, 2:49am UTC](https://www.ruby-forum.com/t/how-would-you-model-this-thanks-for-any-suggestions/152091 "2008-10-30T02:49:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [October 30, 2008, 2:49am UTC](https://www.ruby-forum.com/t/how-would-you-model-this-thanks-for-any-suggestions/152091/1 "2008-10-30T02:49:06Z")

</div>

Hello,

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,  
ms

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [October 30, 2008, 11:19am UTC](https://www.ruby-forum.com/t/how-would-you-model-this-thanks-for-any-suggestions/152091/2 "2008-10-30T11:19:03Z")

</div>

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

Please explain this more detail.

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [October 30, 2008, 11:30am UTC](https://www.ruby-forum.com/t/how-would-you-model-this-thanks-for-any-suggestions/152091/3 "2008-10-30T11:30:48Z")

</div>

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  
relations.

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [October 30, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/how-would-you-model-this-thanks-for-any-suggestions/152091/4 "2008-10-30T12:05:58Z")

</div>

I understand!  
Looking this:

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

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [October 30, 2008, 1:04pm UTC](https://www.ruby-forum.com/t/how-would-you-model-this-thanks-for-any-suggestions/152091/5 "2008-10-30T13:04:09Z")

</div>

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.

[http://cheat.errtheblog.com/s/belongs\_to/](http://cheat.errtheblog.com/s/belongs_to/)  
[http://cheat.errtheblog.com/s/has\_many/](http://cheat.errtheblog.com/s/has_many/)

hope this helps
