Displaying data with has_many association in a table - ruby-on-rails

I have three models like this
class Region < ActiveRecord::Base
attr_accessible :region_name
has_many :districts, dependent: :destroy
end
class District < ActiveRecord::Base
attr_accessible :district_name, :region_id
belongs_to :region
has_many :counties, dependent: :destroy
end
class County < ActiveRecord::Base
attr_accessible :county_name, :district_id
belongs_to :district
has_many :subcounties, dependent: :destroy
end
I want display this data in a table such that i have three columns Region,District and county. Such that a region is diplayed with all its districts and a district with all its counties in their respective columns.
I tried something like this but it didn't work
<table>
<tr>
<th>Region</th>
<th>District</th>
<th>County</th>
</tr>
<% #regions.each do |region|%>
<tr>
<td><%=region.region_name%></td>
<td><%=region.districts%></td>
<td><%=region.districts.counties%></td>
</tr>
<%end%>
</table>
How would i do this correctly?

One issue you're going to run into is that the data structure you've depicted can't be implemented in a true three column table. Rather, you'll need to create a two column parent table where two additional columns are nested within the second column of the parent table. Unfortunately, this will cause your table headers to look a bit off.
However, if you're insistent upon using a table layout, the following should accomplish something akin to what you're looking to do:
<table>
<tr>
<th>Region</th>
<th>District/County</th>
</tr>
<% #regions.each do |region|%>
<tr>
<td><%=region.region_name%></td>
<td>
<table>
<% region.districts.each do |district| %>
<tr>
<td><%= district.district_name %></td>
<td>
<table>
<% district.counties.each do |county| %>
<tr>
<td><%= county.county_name %></td>
</tr>
<% end %>
</table>
</td>
</tr>
<% end %>
</table>
</td>
</tr>
<% end %>
</table>

Related

How to create a table in simple_form_for with 2 nested attributes and checkboxes

I am trying to create a table to grant a model permissions based on an existing relationship with 2 other models.
The idea is something similar to this: https://ibb.co/DRTywF6
For a given brand I would have:
|supplier 1 | supplier 2| supplier 3
clothes_type 1 X
clothes_type 2 X X
clothes_type 3 X
I created a join table "permissions" and edited the models in order to have access from a brand to both suppliers and clothes_types
Displaying is the headers is fine since I just loop through the array of suppliers but I can't find a way to create checkboxes for each pair of clothes_type/supplier for the brand at hand.
I wrote the following
<%= simple_form_for #brand do |f| %>
<table class="table table-hover">
<thead>
<tr>
<th nowrap><%= "Item types" %></th>
<%#suppliers.each do |supplier| %>
<th nowrap><%= supplier.company %></th>
<% end %>
</tr>
</thead>
<tbody>
# that's where I need help :)
</tbody>
My models are as follow:
class Brand < ActiveRecord::Base
has_many :permissions
has_many :suppliers, through: :permissions
has_many :clothes_types, through: :permissions
end
class Supplier < ActiveRecord::Base
has_many :permissions
has_many :brands, through: :permissions
has_many :clothes_types, through: :permissions
end
class ClothesType < ActiveRecord::Base
has_many :permissions
has_many :suppliers, through: :permissions
has_many :brands, through: :permissions
end
class Permission < ActiveRecord::Base
belongs_to :supplier
belongs_to :brand
belongs_to :clothes_type
end
I tried f.collection_check_boxes but it gives me all the suppliers for a given brand and also does not filter by clothes types.
I would like to be able to display the table for each brand. This table would show for a given clothes_type if you have access or not to the maker. If you do, the checkbox would be checked and if you don't, it would be unchecked, leaving you the option to check it and then submit the form to update the permission.
thanks in advance!
It seems like you would want to do something like:
<%= simple_form_for #brand do |f| %>
<table class="table table-hover">
<thead>
<tr>
<th nowrap><%= "Item types" %></th>
<% #suppliers.each do |supplier| %>
<th nowrap><%= supplier.company %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #clothes_types.each do |clothes_type| %>
<td nowrap><%= clothes_type.name %></td>
<% #suppliers.each do |supplier| %>
<td no_wrap>
<% if supplier.clothes_types.include?(clothes_type) %>
# use check_box_tag here
<% end %>
</td>
<% end %>
<% end %>
</tbody>
</table>
<% end %>
On that check_box_tag, I'm not sure what you'll want the name to be as your question is somewhat ambiguous. Also, to set the checked value, you'll probably want to do something like:
supplier.permissions.find_by(clothes_type: clothes_type)
or maybe
supplier.permissions.find_by(clothes_type: clothes_type).try(:granted?)
It's unclear whether the presence of a permission means the permission is granted or whether, perhaps, the permission has an attribute like granted?. Again, your question is ambiguous.

Loop through array in joining tables

am trying to join tables, i have 3 models
class ClassAdviser < ActiveRecord::Base
has_many :feeds
has_many :students
end
class Feed < ActiveRecord::Base
belongs_to :hod
belongs_to :class_adviser
belongs_to :student
end
class Student < ActiveRecord::Base
belongs_to :class_adviser
has_many :feeds
end
How do iterate through the array and get all rows in the joined table,i want to display the details of each student individually.Below is what i tried but i get only details of feeds.
#get_stds = current_class_adviser.students
#get_stds.each do |d|
#students << d.id
end
#students = #students.flatten
for number in #students
#feeds = Feed.joins(:student).where ("student_id = '#{number}'")
end
A few things:
1) I think it is weird that a feed belongs_to a class_advisor. I would change that to be a class_advisor has_many feeds through students.
2) You should be able to just do
#students = #students.flatten
#feeds = #students.map(&:feeds)
To display each student detail along with their respective feed,here is what i did
Controller
#std_feeds = Feed.where(student: current_class_adviser.students)
View
<tbody>
<% #std_feeds.each do |feed| %>
<tr class="table-row">
<td class="table-img">
<p><%=feed.student.first_name%> <%= feed.student.last_name%></p>
<div><%= feed.created_at.strftime('%d %b %Y')%></div>
</td>
<td class="table-text" rowspan="1">
<%= feed.subject %>
<p class='message'><%= feed.message %></p>
</td>
<td class="march">
<h6>DELETE</h6>
</td>
</tr>
<%end%>
</tbody>
This #std_feeds contains both the student details and feed details,all thanks to Rails Association.

Rails association loop display same primary key in row

I have a loop using associations. I'm looking to group by treatment and display in a row.
Each loop has three records for each treatment. The code below this is what i'm producing.
VIEW
<table>
<tr>
<td>Treatment</td>
<td>Date</td>
<td>Count</td>
</tr>
<% #trial.establishmentMethods.order(:treatment_selection_id).each do |data| %>
<tr>
<td><%= data.treatmentSelection.treatment.name %></td> This is reference by treatment_selection_id.
<td><%= data.date %></td>
<td><%= data.count %></td>
</tr>
<% end %>
</table>
This is what i'm hoping to produce. Display the treatment once, then loop the related treatment_selection_id's on the same row.
Here are my models and associations.
class Trial < ApplicationRecord
has_many :assessments, primary_key: 'trial_id'
has_many :establishmentMethods, through: :assessments
end
class EstablishmentMethod < ApplicationRecord
belongs_to :treatmentSelection, primary_key: 'treatment_selection_id', foreign_key: 'treatment_selection_id'
has_many :treatments, through: :treatmentSelection
end
class TreatmentSelection < ApplicationRecord
belongs_to :treatment, primary_key: 'treatment_id'
end
It seems like TreatmentSelection has_many establishmentMethods, so you should add that to the TreatmentSelection model. Then you can do something like:
<% treatment_selections.each do |treatment_selection| %>
<tr>
<td><%= treatment_selection.treatment.name %></td>
<% treatment_selection.establishmentMethods.each do |em| %>
<td><%= em.date %></td>
<td><%= em.count %></td>
<% end %>
</tr>
<% end %>
By the way, it's convention to use snake_case in ruby and it will make using associations easier.

One-to-one association

I have two models and I want to display them in one table.
Model 1:
class Name < ActiveRecord::Base
has_one :employer, :foreign_key => 'application_id'
end
Model 2:
class Employer < ActiveRecord::Base
belongs_to :name, :foreign_key => 'application_id'
end
Controller:
def summary
#name = Name.all
end
I have this in my view:
<% #name.each do |e| %>
<tr>
<td ><%= e.application_id %></td>
<td ><%= e.Name_of_employee%></td>
<td ><%= e.Employer_name%></td>
</tr>
<% end %>
The name only have one employer.
I am getting "undefined method `Employer' for #
"
TYIA!
as the association is defined,
has_one :employer, :foreign_key => 'application-id'
notice the case employer, so it has to be small case, and not class name
<td ><%= e.employer.name %></td> # name or some other attribute you wish to display
Also, including the association first will help eliminate n+1
#name = Name.includes(:employer)

How to use has_many fields in views, rails?

Frnds I am new to rails, here i created two tables caleed stocks and stock_availabilities.
in the stock model
class Stock < ActiveRecord::Base
belongs_to :projects_lkp
has_many :stock_availabilities
validates_presence_of :item
end
In the stock_availabilities model
class StockAvailability < ActiveRecord::Base
belongs_to :stock
validates_presence_of :qty,:add_or_issue,:price, :captured_at, :stock_id,:unit
end
Now my doubt is how to bring the field of stock_availabilties in the views of stock
<% #stock.each do |d| %>
<tr>
<td><%= d.item %></td>
"Here i need to print the values of qty and pricevwhich is in stock_availabilities class"?
</tr>
You are on the right track.
this is what you need:
<% #stock.each do |d| %>
<tr>
<td><%= d.item %></td>
<% d.stock_availabilities.each do |sAV| %>
<td> <%= sAV.qty %> </td>
... <-- You do the other ones here
<% end %>
</tr>

Resources