# Has\_many :through complex form hint request

**URL:** <https://www.ruby-forum.com/t/has-many-through-complex-form-hint-request/147212>\
**Category:** Rails\
**Created:** [August 29, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/has-many-through-complex-form-hint-request/147212 "2008-08-29T12:52:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [August 29, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/has-many-through-complex-form-hint-request/147212/1 "2008-08-29T12:52:05Z")

</div>

hi everyone,  
I’ve seen a lot of examples for complex forms which involves all kinds  
of associations (railscasts, app examples on github, etc.), only  
has\_many :through have been left in the dust a bit.

I’m taking advantage and toying around the new :accessible =\> true for  
mass assignments on new records; however I want to ask if somebody  
have an example on how to use properly the handling of  
has\_many :through associations in edge.

Let’s say in my create action of the interested controller I have  
this:

def create  
…  
@report = @document.reports.build(params[:report])  
if @report.save  
…

Report has\_many Reason :through the join table, so in my form partial  
I’ve the classic fields\_for which does pass the natural  
“report”=\>{“when(1i)”=\>“2008”, “when(2i)”=\>“8”, “when(3i)”=\>“29”,  
“reasons”=\>[{“content”=\>“my reason”}]}

Remember that in Report model there’s the new :accessible for mass  
assignment:  
has\_many :report\_reasons  
has\_many :reasons, :through =\> :report\_reasons, :accessible =\> true

This works well and it does keep one clean line in controller for  
building up the @report instance.  
The problem with has\_many :through is that every time the Report gets  
saved, it creates new entries for every Reason instead of assigning  
one that already exists (of course).

Do you have any hints how to manage cleanly this situation? callbacks,  
real code examples, etc. can help me greatly.
