# Linking many to one id column

**URL:** <https://www.ruby-forum.com/t/linking-many-to-one-id-column/172145>\
**Category:** Rails\
**Created:** [July 15, 2009, 5:04pm UTC](https://www.ruby-forum.com/t/linking-many-to-one-id-column/172145 "2009-07-15T17:04:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [July 15, 2009, 5:04pm UTC](https://www.ruby-forum.com/t/linking-many-to-one-id-column/172145/1 "2009-07-15T17:04:11Z")

</div>

Another newbie question I fear but help as always appreciated.

I have a tools table and a controls table and for each ‘tool’ I allow  
user to specify upto 3 controls.

So the Tools table is :-  
id  
desc  
control\_id1  
control\_id2  
control\_id3

and the Controls table is:  
id  
controldesc

I have linked the tables in their models with  
Tools  
belongs\_to :control

and Control  
has\_many :tools

When I display the Tools rows I want to display the ‘desc’ column from  
the Controls table for each of the control\_id1, control\_id2 and  
control\_id3.

Is this possible or is a relationship link only available on a single  
column in the Tools table that must be called ‘control\_id’ ?

Hope this makes sense.

Thanks

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [July 15, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/linking-many-to-one-id-column/172145/2 "2009-07-15T17:16:47Z")

</div>

Martyn Elmy-liddiard wrote:

> Another newbie question I fear but help as always appreciated.
> 
> I have a tools table and a controls table and for each ‘tool’ I allow  
> user to specify upto 3 controls.
> 
> So the Tools table is :-  
> id  
> desc  
> control\_id1  
> control\_id2  
> control\_id3
> 
> and the Controls table is:  
> id  
> controldesc
> 
> I have linked the tables in their models with  
> Tools  
> belongs\_to :control
> 
> and Control  
> has\_many :tools
> 
> When I display the Tools rows I want to display the ‘desc’ column from  
> the Controls table for each of the control\_id1, control\_id2 and  
> control\_id3.
> 
> Is this possible or is a relationship link only available on a single  
> column in the Tools table that must be called ‘control\_id’ ?

It is possible – check out the options on belongs\_to and has\_many –  
but it’s inadvisable. In general, it is _very_ bad practice to have a  
database design that has repeating fields such as your coltrol\_id[1-3]  
– it will create all sorts of problems down the road. I think you  
should be using a has\_and\_belongs\_to\_many relationship between controls  
and tools.

> Hope this makes sense.
> 
> Thanks

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [July 16, 2009, 10:35am UTC](https://www.ruby-forum.com/t/linking-many-to-one-id-column/172145/3 "2009-07-16T10:35:56Z")

</div>

Can you not access it as

@toolsobject.controldesc ?
