# Tutorial Needed - has\_many through

**URL:** <https://www.ruby-forum.com/t/tutorial-needed-has-many-through/132785>\
**Category:** Rails\
**Created:** [March 24, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/tutorial-needed-has-many-through/132785 "2008-03-24T18:39:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [March 24, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/tutorial-needed-has-many-through/132785/1 "2008-03-24T18:39:49Z")

</div>

I have searched high and low without finding a “good” resource (sample  
code)  
for has\_many through relationships.

Do you have any recommendations?

Thanks,  
Sunny

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [March 24, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/tutorial-needed-has-many-through/132785/2 "2008-03-24T19:06:56Z")

</div>

Sunny B. wrote:

> I have searched high and low without finding a “good” resource (sample  
> code)  
> for has\_many through relationships.
> 
> Do you have any recommendations?
> 
> Thanks,  
> Sunny

Maybe this might help?

> **[\#3 Find Through Association - RailsCasts](http://railscasts.com/episodes/3-find-through-association)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [March 24, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/tutorial-needed-has-many-through/132785/3 "2008-03-24T19:20:32Z")

</div>

This is an old design, but I have Tests, Processes, and data to manage  
for each combination (I think I’ve seen this called “HABTM with  
attributes”). A Test can test many processes, a process can be used in  
many tests. For each Test-Process, there can be success/failure, and  
notes.

class Test \< ActiveRecord::Base

# id

# test\_name

# test\_protocol

has\_many :proctests  
has\_many :aprocesses, :through =\> :proctests  
end

class Proctest \< ActiveRecord::Base

# id

# passfail

# notes

# test\_id

# aprocess\_id

belongs\_to :AProcess  
belongs\_to :Test  
end

class Aprocess \< ActiveRecord::Base

# id

# proc\_name

# process\_notes

has\_many :proctests  
has\_many :tests, :through =\> :proctests  
end

Essentially proctest is a join table with data of its own.

From the Test show view, I can list all the processes it tests:  
\<% if @test.aprocesses  
\<%= render\_partial ‘test\_process\_list’, nil, ‘test\_id’ =\> @test.id ,  
‘testname’ =\> @test.test\_name, ‘processlist’ =\> @test.aprocesses %\>

in that partial, there’s code like:  
\<% for aprocess in processlist %\>  
blah blah blah  
\<% end %\>

and vice-versa:

From the Aprocess show view, I can list all the Tests which test it:

\<%= render\_partial ‘aprocess\_test\_list’, nil, ‘aprocess\_id’ =\>  
@aprocess.id, ‘process’ =\> @aprocess.proc\_name, ‘testlist’ =\>  
@aprocess.tests %\>

in that partial, there’s code like:  
\<% for test in testlist %\>  
blah blah blah  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [March 24, 2008, 9:00pm UTC](https://www.ruby-forum.com/t/tutorial-needed-has-many-through/132785/4 "2008-03-24T21:00:45Z")

</div>

I think I figured out the basics. For those looking for the info in the  
future there are a couple links below.

This link was very helpful creating the model code:  
[http://paulbarry.com/articles/2007/10/24/has\_many-through-checkboxes](http://paulbarry.com/articles/2007/10/24/has_many-through-checkboxes)

This was helpful with the simple view:  
[http://www.railsforum.com/viewtopic.php?id=15844](http://www.railsforum.com/viewtopic.php?id=15844)

This was also somewhat helpful for creating the check boxes:

> **[\#17 HABTM Checkboxes - RailsCasts](http://railscasts.com/episodes/17-habtm-checkboxes)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
