How to use has_many fields in views, rails? - ruby-on-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>

Related

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 sort by count of events [rails]

How to sort this by count of events?
I tried diffrences ways but it still doesn't work:
<tr>
<% User.all.each do |user| %>
<td> <%= user.name %></td>
<td> <%= user.events.count %></br></td>
<% end %>
</tr>
And this is a model:
class User < ActiveRecord::Base
has_many :events
Thanks for answers.
You can achieve it through counter_cache
class Event < ActiveRecord::Base
belongs_to :user, dependent: :destroy,counter_cache: true
end
Add a column events_count to your users table like this
add_column :users, :events_count, :integer, :default => 0
and use it in the view like this
<tr>
<% User.all.each do |user| %>
<td> <%= user.name %></td>
<td> <%= user.events_count %></br></td> #here
<% end %>
</tr>
I would like to suggest to look into this Railscasts,but it is old.
Look in to this plugin And here is an example how it is done.
Hope it helps.

Accessing data from tables

I'm trying to reference a column from another table in my rails application. I want to reference the site_name using the matching site_id columns from both tables(sites and trials).
My controller
def list
#list = Trial.year(params[:year]).order(:region_id)
end
My Models
class Trial < ActiveRecord::Base
attr_accessible :trial_id, :site_id, :year, :trial_type, :region_id
scope :year, ->(year) { where(year: year) }
scope :trial_id, ->(trial_id) { where(trial_id: trial_id) }
belongs_to :site
end
class Site < ActiveRecord::Base
attr_accessible :site_id, :site_name, :region
has_many :trials
end
My View
<table class="table">
<th>Trial Location</th>
<th>Trial Type</th>
<th>Grower</th>
<% #list.each do |list| %>
<tr>
<td>
<%= link_to list.site.site_name, trial_trials_path(trial_id: list.trial_id) %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %>
</td>
</tr>
<% end %>
</table>
Setting up the association in your model does write the getter method for you. The attr_accessibles sets up that method.
Add :site to your Trial's attr_accessible

Displaying data with has_many association in a table

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>

Resources