Linking many to one id column

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

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
[email protected]

Can you not access it as

@toolsobject.controldesc ?