# When to use Has Many and Has and Belongs to many

**URL:** <https://www.ruby-forum.com/t/when-to-use-has-many-and-has-and-belongs-to-many/110581>\
**Category:** Rails\
**Created:** [July 27, 2007, 7:34pm UTC](https://www.ruby-forum.com/t/when-to-use-has-many-and-has-and-belongs-to-many/110581 "2007-07-27T19:34:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [July 27, 2007, 7:34pm UTC](https://www.ruby-forum.com/t/when-to-use-has-many-and-has-and-belongs-to-many/110581/1 "2007-07-27T19:34:27Z")

</div>

I am trying to design an interface that handles user accounts and their  
email aliases.

What is the relationship between a user and their email accounts?

-Ron

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [July 27, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/when-to-use-has-many-and-has-and-belongs-to-many/110581/2 "2007-07-27T20:02:31Z")

</div>

It depends (I know – great answer).

Do you intend to allow only one e-mail address per user?

If so the email should just be an attribute of your user model (an  
email column in the users table).

If you want to allow more than one you could be lazy and just add  
columns for email1, email2, and email3.

You could also have a User model and an Email model where User  
has\_many :emails and Email belongs\_to :user. The emails table would  
be pretty simple – just columns for id and email.

In your controllers you could then use the methods that Rails sets up  
for you automatically like:

u = User.find(1)  
u.emails

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [July 27, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/when-to-use-has-many-and-has-and-belongs-to-many/110581/3 "2007-07-27T20:12:33Z")

</div>

sorry i misunderstand does the email aliases is equal to email address

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [July 27, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/when-to-use-has-many-and-has-and-belongs-to-many/110581/4 "2007-07-27T21:05:40Z")

</div>

On Jul 27, 10:33 am, Ronald V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to design an interface that handles user accounts and their  
> email aliases.
> 
> What is the relationship between a user and their email accounts?
> 
> -Ron

this from Apr, 2006, still good orientation material

[http://blog.hasmanythrough.com/2006/04/20/many-to-many-dance-off](http://blog.hasmanythrough.com/2006/04/20/many-to-many-dance-off)
