# Multi-way join files with types? (resend)

**URL:** <https://www.ruby-forum.com/t/multi-way-join-files-with-types-resend/91475>\
**Category:** Rails\
**Created:** [April 9, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/multi-way-join-files-with-types-resend/91475 "2007-04-09T17:13:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [April 9, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/multi-way-join-files-with-types-resend/91475/1 "2007-04-09T17:13:06Z")

</div>

[I’m still hoping for some help with this, folks… -r]

As part of the Arti ([http://mephisto-ar.cfcl.com](http://mephisto-ar.cfcl.com)) effort,  
I’m looking into creating a database to store information  
on entities and relationships found in Ruby and Rails, as:

Method M1 is defined for Class C1 in File F1.

Method M1 of Class C1 is used by Method M2 in File F2.

Disregarding (for the moment) questions of Ruby ontology,  
let’s look at how this information might be encoded in AR.

I gather that AR wants me to name join files by the ordered  
names of the tables being joined. So, if I have tables  
named “classes”, “files”, and “methods”, I would create a  
join file named “classes\_files\_methods”.

I have two relationships that join the same set of tables,  
so I need a way to tell them apart. I can do so by means of  
a “type” column, with values such as “defines” and “uses”.

However, I’m not at all sure that AR supports multi-table  
joins. In fact, my inquiries indicate that it does not.  
I could use model code to work around this problem, but I’d  
like to keep things as simple as possible.

Am I missing some obvious answer? Can someone suggest some  
code and/or documentation that addresses this issue?

## -r

[http://www.cfcl.com/rdm](http://www.cfcl.com/rdm) Rich M.  
[http://www.cfcl.com/rdm/resume](http://www.cfcl.com/rdm/resume) removed\_email\_address@domain.invalid  
[http://www.cfcl.com/rdm/weblog](http://www.cfcl.com/rdm/weblog) +1 650-873-7841

Technical editing and writing, programming, and web development

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [April 9, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/multi-way-join-files-with-types-resend/91475/2 "2007-04-09T19:49:24Z")

</div>

> As part of the Arti ([http://mephisto-ar.cfcl.com](http://mephisto-ar.cfcl.com)) effort, I’m  
> looking into creating a database to store information on  
> entities and relationships found in Ruby and Rails, as:
> 
> Method M1 is defined for Class C1 in File F1.

For this, I’d make each method belongs\_to a file, each file has\_many  
methods, and each class has\_many methods through files.

> Method M1 of Class C1 is used by Method M2 in File F2.

Here I’d think you’d want a new table, e.g. MethodInvokations, with a  
user\_method\_id, a used\_method\_id, and a file\_id. As far as I know,  
ActiveRecord only supports join tables with two columns, so this would  
need to be a new model object, MethodInvokation, but that’s no big deal,  
the only complication would seem to be that the MethodInvokation object  
belongs to two different Method objects in two different ways. But you  
might do it like:

class MethodInvokation \< ActiveRecord::Base  
belongs\_to :user\_method, :class\_name=\>‘Method’,  
:foreign\_key=\>‘user\_method\_id’  
belongs\_to :used\_method, :class\_name=\>‘Method’,  
:foreign\_key=\>‘used\_method\_id’  
belongs\_to :file  
end

and in the method class, maybe something like:

class Method \< ActiveRecord::Base  
has\_many :invokers, :class\_name=\>‘MethodInvokation’,  
:foreign\_key=\>‘used\_method\_id’  
has\_many :invokations, :class\_name=\>‘MethodInvokations’,  
:foreign\_key=\>‘user\_method\_id’  
end

I’m no ActiveRecord expert, so take with a grain of salt, but this is  
how I’d start to model it.

- donald
