Matthew Williams
2008-Mar-12 17:35 UTC
Building a view for a 3 model has_many relationship
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 %>
<div>
<p>
<%= check_box_tag "user[charge_ids][]", charge.id,
@user.charges.include?(charge) %>
<%= [charge.project_number,
charge.primary_number].join('''') %> - <%charge.label
%>
</p>
</div>
<% 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!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 Williams < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 %> > <div> > <p> > <%= check_box_tag "user[charge_ids][]", charge.id, > @user.charges.include?(charge) %> > <%= [charge.project_number, charge.primary_number].join('''') %> - <%> charge.label %> > </p> > </div> > <% 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! > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Williams
2008-Mar-12 18:13 UTC
Re: Building a view for a 3 model has_many relationship
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 Williams <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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---