问题
I have a parent table, child Table, funding table. Parent logs in, add his children and then apply for funding for each child.
parent.rb
class Parent < ApplicationRecord
has_many :children, dependent: :destroy
has_many :secondaryparents, dependent: :destroy
end
funding.rb
class Funding < ApplicationRecord
has_many :organisations, dependent: :destroy
belongs_to :child
end
child.rb
class Child < ApplicationRecord
belongs_to :parent
has_many :fundings, dependent: :destroy
end
Parent has a field as admin. if set as true parent acts as admin. application.html.erb is the view of admin. I am able to display all the primary parents and their children in front of each other. I need help with displaying the funding applications of the children in front of them.
application.html.erb
<div class="row">
<!-- .main_column_css -->
<div class="col-sm-9 col-xs-12">
<div class="content" role="main" id="main-content">
<article>
<div ><h1>Application Status</h1>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Primary Parent Name</th>
<!-- <th>Financial Status</th> -->
<th>Child Name</th>
<th>Activity</th>
<th>Organisation</th>
<th>Activity Start Date</th>
<th>Date Submitted</th>
<th>Amount Requested</th>
<th>Funds Available</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<% @parents.each do |parent| %>
<% parent.children.each do |child| %>
<tr>
<td><%= parent.parent_1_firstname %></td>
<td><%= child.firstname %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
<div class="clearfix"></div>
</div>
</article>
</div>
<!-- .content -->
</div>
</div>
financial status, activity, activity start date, date submitted, amount requested, funds available are all fields of funding table. Funding table has the child_id. child table has parent_id. Kindly help
Update
<% @parents.each do |parent| %>
<% parent.children.each do |child| %>
<% child.fundings.each do |funding| %>
<tr>
<td><%= parent.parent_1_firstname %></td>
<td><%= child.firstname %></td>
<td><%= funding.type_of_activity %></td>
</tr>
<% end %>
<% end %>
<% end %>
回答1:
you can do child.fundings.each do |funding| and then do all the attributes for funding.
来源:https://stackoverflow.com/questions/50805826/view-for-admin-in-rails