Tutorial Needed - has_many through

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

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?

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 %>

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

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

This was also somewhat helpful for creating the check boxes: