# :has\_many and :belongs\_to: how do they work

**URL:** <https://www.ruby-forum.com/t/has-many-and-how-do-they-work/65740>\
**Category:** Rails\
**Created:** [August 21, 2006, 2:11am UTC](https://www.ruby-forum.com/t/has-many-and-how-do-they-work/65740 "2006-08-21T02:11:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 21, 2006, 2:11am UTC](https://www.ruby-forum.com/t/has-many-and-how-do-they-work/65740/1 "2006-08-21T02:11:31Z")

</div>

I am new to rails, and don’t know how :has many and :belongs to work,  
but I really like the idea of being able to say, for example  
email.accounts, and I was wonderign what the requirements are. Does this  
system work off of foreign keys, or does it work off of a system like  
this  
for example  
email table

* * *

email account

Accounts table

* * *

name email

meaning, that it matches the two email fieldnames?

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 21, 2006, 6:42am UTC](https://www.ruby-forum.com/t/has-many-and-how-do-they-work/65740/2 "2006-08-21T06:42:11Z")

</div>

> Accounts table
> 
> * * *
> 
> name email
> 
> meaning, that it matches the two email fieldnames?

By default, ActiveRecord expects foreign keys to be named

\_id.  
In your example, account would have a field called email\_id of type  
:integer. You can then use belongs\_to and has\_many.

I would also suggest you spend some time trawling the web for  
tutorials/books. There are many out there and they’ll help you  
understand how Rails is put together.

Max

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 21, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/has-many-and-how-do-they-work/65740/3 "2006-08-21T16:30:49Z")

</div>

Thanks for your help and time. I am a novice at both rails, and  
databases, so sometimes I can code things, but I don 't understand how  
they work.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 21, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/has-many-and-how-do-they-work/65740/4 "2006-08-21T19:36:06Z")

</div>

Ben, I highly suggest you get the book Agile Development on Rails, and  
read  
it. It will give you a good understanding of how Rails works. Without a  
good  
understanding of the underlying technology, you’ll probably keep running  
into walls when coding. But once you understanding it, coding becomes  
fairly  
simple.

On 8/21/06, Ben [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
“Impossible is nothing.”
