show_path in Rails table not using right :id? - ruby-on-rails

Rails newbie here, writing a sample app that has the following three models and relationships:
Sales person:
class Salesperson < ActiveRecord::Base
has_many :clients
Client:
class Client < ActiveRecord::Base
has_many :orders
belongs_to: salesperson
Orders:
class Order < ActiveRecord::Base
belongs_to :client
On the page clients/show.html.erb I have a partial that renders the following:
<table>
<tr>
<th>Name</th>
<th>Total Orders</th>
<th>Email</th>
<th></th>
</tr>
<% #salesperson.clients.each do |client| %>
<tr>
<td><%= client.full_name %></td>
<td><%= client.orders.count %></td>
<td><%= client.email %></td>
<td><%= link_to 'View Client', client_path %></td>
</tr>
<% end %>
</table>
Resources are all nested, and the page seems to work except for one thing:
Everything is dynamic but the client path: the client name, orders, emails are all displayed for the salesperson, but the view client link seems to always point at a clients/:id link where :id is the salesperson id but not the client ID.
E.g. because the salesperson :id is 1, all the client paths will point to clients/1 down the whole table.
How do I make the client link route correct and dynamic like the rest of the table?

Try with
<td><%= link_to 'View Client', client_path(client) %></td>
or simpler
<td><%= link_to 'View Client', client %></td>
client_path need a parameter that is a Client or an id.

Related

How can i get User name in a table where user_id is used as foreign key

I am using Devise gem for user Authentication. User id is as foreign key in article table
How can i get writer name through User_id in a view Show_article.html.erb
I can access user_id in show_article.htmlerb
I have tried to make a custom function in article controller but could not get the desired output
your Article model should look like this:
class Article << ActiveRecord::Base
belongs_to :user
#....some more lines
end
your User model should look like this:
class User << ActiveRecord::Base
has_many :articles
#....some more lines
end
and your show.html.erb should say:
#....your rest of code
<%= #article.try(:user).try(:name) %>
#....your rest of code
This will skip user name if you article doesn't have any user. It looks like your article doesn't have user_id or you haven't defined your relations correctly.
It might be the case that some of your article do not have any user. so
you can do
<%= #article&.user&.name %> in show page
and in the index page
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article&.user&.name%></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article),
method: :delete,
data: { confirm: 'Are you sure you want to delete this article?' } %></td>
</tr>
<% end %>
but make sure you have ruby 2.3.0 or higher

Trying to display records from chained models on Rails 5.2

Following on from this question
class CoffeeRoast < ApplicationRecord
has_many :coffee_blends
has_many :coffee_beans, through: :coffee_blends
has_one :country, through: :coffee_beans
end
class CoffeeBean < ApplicationRecord
has_many :coffee_blends
has_many :coffee_roasts, through: :coffee_blends
belongs_to :country
end
class Country < ApplicationRecord
has_many :coffee_beans
end
class CoffeeBlend < ApplicationRecord
belongs_to :coffee_bean
belongs_to :coffee_roast
end
I am able to show the related coffee_beans on the coffee_roasts show page, and also the country, however, I can not work out how to display them correctly.
My current code is attempting to show the bean and its related country on the same row on the table, however the top is blank and the two countries are both in the second row.
coffee_roasts/show.html.erb
<h1 class="display-3"><%= #coffee_roast.name %></h1>
<p>
by <h2 class="display-6"><%= #coffee_roast.roaster.roaster_name %></h2>
</p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Bean</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<% #coffee_roast.coffee_blends.each do |blend| %>
<tr>
<th><%= blend.coffee_bean.name %>
<% end %></th>
<th><%= #coffee_roast.coffee_beans.map {|cb| cb.country.country_name }.join(', ') %></th>
</tr>
</tbody>
</table>
Bean | Country
El Martillo |
Finca La Cumbre | El Salvador, Guatemala
El Salvador show be in-line with the El Martillo bean.
My approach feels quite wrong here, but I'm pretty new to working with multiple levels of models so still learning.
How can I get the beans associated country to display alongside the bean?
This was actually a lot simpler than I thought. I've found the below works perfectly. No need to map an array.
<tbody>
<% #coffee_roast.coffee_blends.each do |blend| %>
<tr>
<td><%= blend.coffee_bean.name %></td>
<td><%= blend.coffee_bean.country.country_name %></td> #this bit was the original concern.
<td><%= blend.coffee_bean.variety %></td>
<td><%= blend.coffee_bean.process %></td>
<% end %>
</tr>
</tbody>
<tbody>
<% #coffee_roast.coffee_blends.each do |blend| %>
<tr>
<td><%= blend.coffee_bean.name %></td>
<td><%= #coffee_roast.coffee_beans.map {|cb| cb.country.country_name }.join(', ') %></td>
</tr>
<% end %>
</tbody>
try and see if this works?

Rails counting rows with has_many relation

I have:
class Constituency < ActiveRecord::Base
has_many :votes
end
class Vote <ActiveRecord::Base
belongs_to :constituency
end
Now, what I want to do is to create a table, that will show me Name of constituency, number of voters (which is easy, as it is part of the table Constituency) and number of votes that were given in this constituency.
The beginning looks like this:
<% #constituencies.each do |constituency| %>
<tr>
<td><%= constituency.name %></td>
<td><%= constituency.voters %></td>
So now comes my question: how can I count all rows in Votes, but with division based on constituency?
constituency.votes.count should give you the correct count for each different constituency. Hope this helps.
<% #constituencies.each do |constituency| %>
<tr>
<td><%= constituency.name %></td>
<td><%= constituency.voters %></td>
<td><%= constituency.votes.count %></td>

<td> tags inside view disappearing when declaration is made on object's associated model

Been perplexed on this one for a while now. It seems like a simple oversight but I cannot get past it.
I have a basic table element to list jobs with each row representing an record in the jobs table.
<% if #jobs.any? %>
<thead>
<tr>
<th>Booking ID</th>
<th>Cleaner</th>
<th>Address</th>
<th>Status</th>
<th></th>
<th></th>
</tr>
</thead>
When I instantiate the associated model of 'address' assigned to 'addy', the row elements immediately disappear on refresh. Am I missing something here?
<% #jobs.each do |job| %>
<% job.address do |addy| %>
<tr>
<td><%= job.id %></td>
<td><%= job.jrecipient_id %></td>
<td><%= addy.label %></td>
<td>No status yet</td>
<td><%= link_to "Change Appointment", '#' %></td>
<td><%= link_to "Cancel Cleaning", job, method: :delete, data: { confirm: "You sure?" } %> </td>
</tr>
<%#distance_of_time_in_words(job.created_at, Time.now)%>
<% end %>
<% end %>
EDIT: Some more information
class Address < ActiveRecord::Base
belongs_to :job
end
class Job < ActiveRecord::Base
has_one :address
end
Since you have ''undefined methodlabel' for nil:NilClass'` ' , it means the job you want to display doesn't have an address ( address attribute of Job is Nil). Do you require an address in your validations ?
Otherwise , try to create a job with an address in the console (rails console) and display it after see what you get.
Show us your Job model maybe...
For the case when job has many addresses
<% job.address.each do |addy| %>
or for the case when job has one address
<% addy = job.address %>

Identifying Model Instance by Two Relations

I'm trying to pick out an instance of a model ("Package") by its relation to two other models to which it belongs ("Cut" and "Animal"). While there are, say many packages with :cut_id 3 and many with :animal_id 4, there should only be one with both, and I want to pick that one out and display its contents in a table.
I've tried the following DIY mess, and it's not really working. (cutfind is a method I created that I know works for calling out all of the cuts associated with the given animal.)
<% #animal.cutfind.each do |cut| %>
<tr>
<td><%= cut.name %></td>
<td><%= number_to_currency(cut.price) %></td>
<td><%= cut.package_weight %> lb</td>
<% #a = Package.where(:animal_id => #animal.id) %>
<% #pset = #a.where(:cut_id => cut.id) %>
<% #pset.each do |p| %>
<td><%= p.original %></td>
<td><%= p.left %></td>
<% end %>
</tr>
<%end%>
Any idea how to do this [better]? Thanks.
Update: I tried this other DIY mess and am getting the same problem (the cells aren't even being created, which leads me to believe that #pset is empty).
This is in my animal model:
def packagefind
Package.where(:animal_id => self.id)
end
And then I changed the above like so:
<td><%= cut.package_weight %> lb</td>
<% #pset = #animal.packagefind.where(:cut_id => cut.id) %>
<% #pset.each do |p| %>
<td><%= p.original %></td>
<td><%= p.left %></td>
<% end %>
Rails will automatically generate methods to help you find the associated records if you define the following relations:
class Animal
has_many :cuts
has_many :packages, :through => :cuts
end
class Cut
belongs_to :animal
belongs_to :package
end
class Package
has_many :cuts
has_many :animals, :through => :cuts
end
In your controller, the following line will eager load all the records you will need in your view:
#animal = Animal.includes(:cuts => :package)
Your view can then be shortened to:
<% #animal.cuts.each do |cut| %>
<tr>
<td><%= cut.name %></td>
<td><%= number_to_currency(cut.price) %></td>
<td><%= cut.package_weight %> lb</td>
<td><%= cut.package.original %></td>
<td><%= cut.package.left %></td>
</tr>
<%end%>
As I'm not able to comment on your post, I take a guess:
You have the folllowing architecture:
Cut -> Package <- Animal
In this, "->" and "<-" are one-to-many relationships so that
class Package < ActiveRecord::Base
has_many :cuts
has_many :animals
end
So, you want "the" package, that has Cut with id 3 and Animal id 4.
Did you try:
x = Product.select { |product| product.cuts.include?(Cut.find(3)) }.select{ |product| product.animals.include?(Animal.find(4)) }
?
EDIT: I first suggested to you use
Product.find_by_product_id_and_animal_id()
which didn't work but showed the OP the way to do it

Resources