# How would you do a multitable cross reference select box in Rails?

**URL:** <https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043>\
**Category:** Rails\
**Created:** [December 23, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043 "2007-12-23T23:16:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bsevans](https://www.ruby-forum.com/u/bsevans)\
**Post date:** [December 23, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043/1 "2007-12-23T23:16:22Z")

</div>

I’ve done this in other languages, but how would you handle the  
following issue in Rails?

Assume database tables a, b and c.  
Each table has id,name,text fields  
(and yes, there are reasons why they are separate tables -  
I’m simplifying here)

Any record in any table can cross-reference one or more records  
in that table or another table. The cross reference tables are  
a\_a, a\_b, a\_c, b\_b, b\_c, c\_c [fields are as expected a\_id,b\_id, etc)

So assume record 1 in table a is referenced by:  
record 22 in table a  
record 347 in table b and  
record 19 in table c.

When a record is displayed, I have a dropdown select box that shows  
the name of any cross references. The contents are generated by a  
database view created by multiple selects combined with union.

The obvious hurdle in a select box is returning not just the id of the  
selected item, but also the table in which that particular item is  
located.  
In my current (non-ruby, non-rails) implementation, this is solved by  
concatenating the table name with a separator and the record id

The select box thus returns the combined value which is then exploded  
into its parts so that the webapp can display the desired item. This  
isn’t difficult when done manually, but what would be the appropriate  
way of handling the issue in the “Rails” way?

---

<div class="post-metadata">

**Author:** ![bsevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bsevans](https://www.ruby-forum.com/u/bsevans)\
**Post date:** [December 24, 2007, 11:13am UTC](https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043/2 "2007-12-24T11:13:16Z")

</div>

On Dec 23, 2007 11:20 PM, Bryan E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The select box thus returns the combined value which is then exploded  
> into its parts so that the webapp can display the desired item. This  
> isn’t difficult when done manually, but what would be the appropriate  
> way of handling the issue in the “Rails” way?

Are you free to modify the database?  
I’d probably try making a new model CrossReference with it’s own table  
cross\_references.  
This table can hold a\_id, b\_id and c\_id, and your A, B and C each  
has\_many :cross\_references.  
If it makes sense for your models, A has\_many :bs, :through =\>  
:cross\_references etc.  
I don’t know exactly what the details are for referencing the same  
table (what you have in a\_a, b\_b and c\_c), but I imagine it’s doable.

You’d have to add columns (d\_id) to the cross\_references table when  
you add a table (d) to be cross referenced. But on the other hand, in  
your current setup, you’d have to add an increasing number of new  
reference-tables a\_d, b\_d, c\_d and d\_d.

hmm… hope that helps  
merry x-mas

- Martin

---

<div class="post-metadata">

**Author:** ![bsevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bsevans](https://www.ruby-forum.com/u/bsevans)\
**Post date:** [December 24, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043/3 "2007-12-24T18:03:00Z")

</div>

I could modify the database to add a multitable linking table - I  
can’t reduce the current number of linking tables because it would  
break existing apps that I don’t control. The three real data tables  
have a lot of fields that are not in common and serve different  
purposes, so they can’t really be combined.

Even if I have a single multi-table linking table, I’m not sure that  
solves the problem of how the select box passes the correct info.

A has\_many :bs, :through =\> :cross\_references  
A has\_many :cs, :through =\> :cross\_references

I’m still puzzled at what the return from the single select box will  
look like.

Bryan

---

<div class="post-metadata">

**Author:** ![bsevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bsevans](https://www.ruby-forum.com/u/bsevans)\
**Post date:** [December 25, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043/4 "2007-12-25T13:25:52Z")

</div>

On Dec 23, 2:20 pm, Bryan E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> selected item, but also the table in which that particular item is located.  
> In my current (non-ruby, non-rails) implementation, this is solved by  
> concatenating the table name with a separator and the record id
> 
> The select box thus returns the combined value which is then exploded  
> into its parts so that the webapp can display the desired item. This  
> isn’t difficult when done manually, but what would be the appropriate  
> way of handling the issue in the “Rails” way?

It almost sounds like polymorphic table inheritance? (from the agile  
book).  
I’m having a hard time visualizing what the use-case would be (ie:  
Employee \< Person; Manager \< Person; Mentor \< Person subclassing?)  
and how that would look in the select box itself.

---

<div class="post-metadata">

**Author:** ![bsevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bsevans](https://www.ruby-forum.com/u/bsevans)\
**Post date:** [December 25, 2007, 12:21am UTC](https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043/5 "2007-12-25T00:21:27Z")

</div>

On Dec 24, 9:02 am, bryane [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m still puzzled at what the return from the single select box will look like.

> In my current (non-ruby, non-rails) implementation, this is solved by  
> concatenating the table name with a separator and the record id

Why can’t you use this method in Rails with options\_for\_select? You  
can pass in an array of 2-element arrays, each of which contains the  
text to display and the value to return. You can make the values just  
like you did before.

///ark

---

<div class="post-metadata">

**Author:** ![bsevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bsevans](https://www.ruby-forum.com/u/bsevans)\
**Post date:** [December 25, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043/6 "2007-12-25T21:45:47Z")

</div>

Think of the use case this way (again it is somewhat simplified)  
Table A is text of laws relevant to our company  
Table B is external commentary on laws (think articles)  
Table C is internal commentary on laws (think internal notes).

Any law can be flagged with one or more references to another law, or  
commentaries from either commentary table.  
Any commentary (external or internal) can be flagged with one or more  
references to one or more laws, or commentary from either commentary  
table.

According to our lawyers, we need to keep the external commentary  
separate from our internal commentary in order to avoid fights with  
our external suppliers.

Bryan.

---

<div class="post-metadata">

**Author:** ![bsevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bsevans](https://www.ruby-forum.com/u/bsevans)\
**Post date:** [December 25, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/how-would-you-do-a-multitable-cross-reference-select-box-in-rails/124043/7 "2007-12-25T23:25:17Z")

</div>

Perhaps a commentaries table with ‘type’ attribute (externel or  
internal), and an foreign key that references the laws table and its  
id.

Would look something like:

create\_table commentaries:  
id =\> :integer,  
type =\> :string, #=\> (internal or external)  
comment =\> :text  
law\_id =\> :integer #=\> references laws.id  
end

create\_table laws:  
id =\> :integer,  
name =\> :string,  
text =\> :text  
end
