# Has\_many\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/has-many-and-belongs-to-many/117333>\
**Category:** Rails\
**Created:** [October 16, 2007, 1:14am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-many/117333 "2007-10-16T01:14:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [October 16, 2007, 1:14am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-many/117333/1 "2007-10-16T01:14:20Z")

</div>

I read this:  
[jrhicks.net](http://jrhicks.net/Projects/rails/has_many_and_belongs_to_many.pdf)

It is from 2005, so some of it has old screenshots (and it looks like  
Windows).

Do I really create that third table on my own? Rails doesn’t  
automagically create it?

I have students and parents. Students can have more than one parent  
and a parent can have more than one student. So I have to create a  
students\_parents table to track the connections? How will rails know  
it is that instead of a parents\_students table?

Thanks,  
–Colin

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [October 16, 2007, 1:21am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-many/117333/2 "2007-10-16T01:21:20Z")

</div>

On 10/15/07, Colin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have students and parents. Students can have more than one parent  
> and a parent can have more than one student. So I have to create a  
> students\_parents table to track the connections? How will rails know  
> it is that instead of a parents\_students table?

Actually, rails would want a parents\_students table, it puts the model  
names in sort order.

And yes, you need to create the table, but nowadays you can use a  
migration instead of hand coded sql

CreateParentsStudents \< ActiveRecord::Migration

```
def self.up
    create_table :parents_students, :id => false do | t |
        t.column :parent_id, :integer
        t.column :student_id, :integer
    end

```

end

def self.down  
drop\_table :parents\_students  
end  
end

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [October 16, 2007, 1:57am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-many/117333/3 "2007-10-16T01:57:00Z")

</div>

Okay, thanks. I did that. Perfect.

–Colin
