# Building a view for a 3 model has\_many relationship

**URL:** <https://www.ruby-forum.com/t/building-a-view-for-a-3-model-has-many-relationship/131717>\
**Category:** Rails\
**Created:** [March 12, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/building-a-view-for-a-3-model-has-many-relationship/131717 "2008-03-12T18:35:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [March 12, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/building-a-view-for-a-3-model-has-many-relationship/131717/1 "2008-03-12T18:35:17Z")

</div>

I’m working with three models:

- Charge. has\_many :budget\_plans
- BudgetPlan. belongs\_to :charges, :rollups
- Rollup. has\_many :budget\_plans

This works out perfectly. A budget plan contains the ID of a charge and  
rollup and then there’s a 3rd attribute for hours. So working with data  
is no problem.

My budget plan scaffold works nicely so I can select a charge, select a  
rollup and enter in hours and it works great and then in a variety of  
areas of my application I can reference any budget plans associated with  
a specific charge.

On my edit view for a charge I would like a list of all of the rollups  
(there’s a label attribute in my rollup model) with a text field next to  
it that is the hours associated with the budget plan. So when editing a  
charge I am seeing a list of every item in the rollup model with a text  
field either containing a value if one exists in the BudgetPlan model or  
just have an empty text field so one can be entered if need be.

I guess the pseudo code would look something like…

Charge edit view:  
@rollup = Rollup.find(:all)

form  
for rollup in @rollup  
rollup label: budget\_plan.hours text field  
end  
end form

And result with:

Charge: 1

Rollup Hours  
1 25  
2  
3 89

Then I can see all of the rollups and if I have a budget for one I can  
enter the hours and those then get posted to my budget\_plan model  
otherwise they just remain blank.

I’m doing something similar in another view that works fine with a habtm  
relationship:  
\<% for charge in @charges %\>

\<%= check\_box\_tag "user[charge\_ids][]", charge.id, @user.charges.include?(charge) %\> \<%= [charge.project\_number, charge.primary\_number].join('') %\> - \<%= charge.label %\>

\<% end %\>

That is used to determine which charges a user has access to, so when on  
the edit screen for a user, it lists all the charges with a check box  
and when the form is submitted it then modifies my join table with all  
of the ID’s that need updating because of the has\_and\_belongs\_to\_many  
:charges defined in my user model.

I know this is a pretty simple task but I can’t wrap my brain around it  
and could use another set of eyes… Thanks! Feel free to ask  
questions if you need any clarifying but I think this is a pretty  
straight forward task.

Thanks!

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [March 12, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/building-a-view-for-a-3-model-has-many-relationship/131717/2 "2008-03-12T18:59:57Z")

</div>

in model:  
Charge.  
has\_many :budget\_plans  
has\_many :rollups, :through =\> :budget\_plans

in Controller:

@charge = Charge.find(1, :include =\> :rollups)

In View:

@charge.rollup.fieldname

Adam

On Wed, Mar 12, 2008 at 1:35 PM, Matthew W. \<

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [March 12, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/building-a-view-for-a-3-model-has-many-relationship/131717/3 "2008-03-12T19:13:48Z")

</div>

AD wrote:

> in model:  
> Charge.  
> has\_many :budget\_plans  
> has\_many :rollups, :through =\> :budget\_plans
> 
> in Controller:
> 
> @charge = Charge.find(1, :include =\> :rollups)
> 
> In View:
> 
> @charge.rollup.fieldname
> 
> Adam
> 
> On Wed, Mar 12, 2008 at 1:35 PM, Matthew W. \<

So this is a bit of an improvement over what I had before but the issue  
of modifying the records in the BudgetPlan model from the Charge views  
isn’t solved.

Ultimately I would like every record from rollup to be displayed on the  
edit view for the charge model. If I decide to enter a value for a  
specific rollup and submit the form a record is created in the  
BudgetPlan model containing the ID of the charge, the ID of the rollup  
and the hours value.

Still hacking away…
