# Belongs\_to and has\_many

**URL:** <https://www.ruby-forum.com/t/belongs-to-and-has-many/58778>\
**Category:** Rails\
**Created:** [April 25, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/58778 "2006-04-25T21:19:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blake](https://www.ruby-forum.com/u/blake)\
**Post date:** [April 25, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/58778/1 "2006-04-25T21:19:54Z")

</div>

```
I know this has been rehashed many times, but I don't get it.

```

I can’t find it documented to were I understand all the parts. I can  
make this work using Rails database design conventions, but in this case  
I am not able to do that.  
I find examples, but the ones I find do not follow the conventions.  
They also do not give enough information to understand what is going on.

In the model:  
What is the “association”? Is it the field name minus the \_id?  
:foreign\_key is the key in the other table.  
:class\_name is the name of the model of the other table.  
------createtables.sql  
CREATE TABLE admins(  
id INTEGER PRIMARY KEY,  
admin VARCHAR(32) NOT NULL,  
);  
CREATE TABLE machines(  
id INTEGER PRIMARY KEY,  
padmin\_id REFERENCES admins(admin),  
sadmin\_id REFERENCES admins(admin)  
)  
-------models/machine.rb------------  
class Machine \< ActiveRecord::Base  
belongs\_to :padmin, :class\_name =\> “admin”, :foreign\_key =\> “id”  
belongs\_to :sadmin, :class\_name =\> “admin”, :foreign\_key =\> “id”  
end  
-------models/admin.rb--------------  
class Admin \< ActiveRecord::Base  
has\_many :machines, :foreign\_key =\> ‘padmin\_id’  
has\_many :machines, :foreign\_key =\> ‘sadmin\_id’  
end  
-------views/\_list\_stripes.rhtml-----  
If it were a straight association machine.admin\_id and admin.id, then  
this works

## \<%= list\_stripes.admin ? list\_stripes.admin[“admin”] : “UnAssigned” %\>

I would be happy if I could understand it well enough that I could use  
it without the compilcation of “sadmin\_id”.

Blake

---

<div class="post-metadata">

**Author:** ![blake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blake](https://www.ruby-forum.com/u/blake)\
**Post date:** [April 25, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/58778/2 "2006-04-25T22:47:40Z")

</div>

I’m not sure i quite understand your data model…

If it’s: “One admin can have many machines, One machine belongs to many  
admins” then you should have a joiner table:

administrators administrators\_machines machines

* * *

id \<---- administrator\_id |–\> id  
admin machine\_id – machine

* * *

Your models will then reflect this relationship:

class Machine \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :administrators  
end

class Administrator \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :machines  
end

Rails will automatically map everything together for you. The  
associations should be avaliable as a collection in the models:

@administrator.machines.find(:all)

Hopefully this will be helpful - i apologise if i’ve mis-understood your  
problem :0)

Steve

---

<div class="post-metadata">

**Author:** ![blake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blake](https://www.ruby-forum.com/u/blake)\
**Post date:** [April 26, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/58778/3 "2006-04-26T22:38:21Z")

</div>

Steve,  
I think you understand my data. I was trying to avoid habtm. I  
haven’t found a way to associate data with each join. We have primary  
and secondary admins for each machine.

habtm does not deal with this:

administrators administrators\_machines machines

* * *

id \<---- administrator\_id |–\> id  
admin machine\_id – machine  
[primary | secondary]

* * *

```
In the mean time I would like to figure out the correct syntax for

```

describing the models and listing the data with just belongs\_to and  
has\_many. I would drop the secondary admin out of the model. But instead  
of renameing things I would like to know how to do it without following  
rails naming conventions.

Does join through allow this type of model?

Blake

---

<div class="post-metadata">

**Author:** ![blake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blake](https://www.ruby-forum.com/u/blake)\
**Post date:** [April 27, 2006, 12:17am UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/58778/4 "2006-04-27T00:17:50Z")

</div>

Blake Lewis wrote:

> Does join through allow this type of model?
> 
> Blake

The :through approach requires creating a new Model in ghe middle, say  
‘adminship’,

so administrator has\_many :adminships and has\_many :machines :though  
=\> ‘adminships’  
and machine has\_many :adminships and has\_many :administrators :through  
=\> ‘adminships’.

The adminships table, in addition to administrayor\_id and machine\_id  
will have, say a priority column indicating primary or secondary

The Adminship model would belongs\_to :machine and belongs\_to  
:administrator.

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

Does this help ?

Alan
