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>
Related
I have 3 relevant tables to this problem: 'djobs', 'rigs', and 'photographers'. A djob has_many rigs, and a rig has_one photographer. Currently on my djob index screen I have a table where I am listing djob values and rig values, but I don't know how to also display photographer values.
I'm not really sure where to begin with this, so all I've tried is ensuring the models are associated correctly.
Here's my code:
djobs controller:
def index_photography
#djobs = Djob.order(:id).joins(:rigs, :photographers)
end
djobs model:
class Djob < ActiveRecord::Base
has_many :rigs
has_many :photographers, :through => :rigs
accepts_nested_attributes_for :rigs, allow_destroy: true
accepts_nested_attributes_for :photographers
end
djobs index view:
<tbody>
<% #djobs.each do |djob| %>
<tr>
<td><%= link_to 'Edit', edit_djob_path(djob, q: params[:q], :type => "photography") %></td>
<td class="dotted" ><%= djob.jobtype %></td>
<td class="dotted" ><% djob.rigs.each do |rig| %><%= rig.name %></br><% end %></td>
<td class="dotted" ><% djob.rigs.photographers.each do |photographer| %><%= photographer.name %></br><% end %></td>
</tr>
<% end %>
</tbody>
rig model:
class Rig < ActiveRecord::Base
belongs_to :djob
has_one :photographer
accepts_nested_attributes_for :photographers
end
photographer model:
class Photographer < ActiveRecord::Base
belongs_to :rig
end
This code gives me the error: "No association found for name `photographers'. Has it been defined yet?". All I want is for the table to display the photographer's name associated with that particular rig.
Edit: I tried the suggest code from an answer below using this as my code:
<td class="dotted" ><% djob.photographers.each do |photographer| %><%= photographer.name %></br><% end %></td>
and I also fixed my association model code for rigs and djobs.
And while it didn't give me any errors as I had before, there are no photographers showing at all. It's totally blank now. I don't know how to fix this issue. I know for a fact photographers are associated to rigs that are associated to the djob. And the rigs are displaying. So I'm not sure why the photographers aren't showing up at all for me. I'm open to new ideas.
I suppose the problem is here
<td class="dotted" ><% djob.rigs.photographers.each do |photographer| %><%= photographer.name %></br><% end %></td>
should probably be (to display all photographers associated with djob)
<td class="dotted" ><% djob.photographers.each do |photographer| %><%= photographer.name %></br><% end %></td>
or
<td class="dotted" ><% djob.rigs.each do |rig| %><%= rig.photographer.name %></br><% end %></td>
to display single photographer for each rig.
The reason of an error is that your djob model has many photograpers (plural form), but rig has only one photograper.
I have two tables
sample
has_many :abundances
self.primary_key = :sample_id
and
abundance
has_many :samples
self.primary_key = :sample_id
In abundances controller i have a ransack search
def index
#search = Abundance.ransack(params[:q])
#abundances = #search.result
#abundancez = #abundances.paginate(:page => params[:page],:per_page => 100)
end
end
in the abundances view, I have a filtered table based on the ransack parameters.
<%= will_paginate #abundancez%>
<% #abundancez.each do |abundance| %>
<td><%= abundance.sample_id %><td>
<td><%= abundance.length %><td>
<td><%= abundance.eff_length%><td>
<td><%= abundance.est_counts%><td>
<td><%= abundance.tpm%><td>
<% end %>
The sample table has a field, race that i want to pull up in the abundances view when via corresponding to the filtered parameters above.
Ive tried to use pluck in the view
<%= will_paginate #abundancez%>
<% #abundancez.each do |abundance| %>
<td><%= abundance.sample_id %><td>
<td><%= abundance.length %><td>
<td><%= abundance.eff_length%><td>
<td><%= abundance.est_counts%><td>
<td><%= abundance.tpm%><td>
<td><%= samples.pluck(abundance.samples_id,:race)%></td>
but i get an error. Not sure if I'm going about this the right way or if I the syntax is incorrect.
Thanks!
The thing here is that you defined that an abundance has_many samples, so you can't return the value of the race for one sample like you are doing here.
as you said in your comments, it seems that your associations are wrong, change your abundance model from has_may, to belongs_to
belongs_to :sample
and then on your view, you can return the race value like this
<%= will_paginate #abundancez%>
<% #abundancez.each do |abundance| %>
<td><%= abundance.sample_id %><td>
<td><%= abundance.length %><td>
<td><%= abundance.eff_length%><td>
<td><%= abundance.est_counts%><td>
<td><%= abundance.tpm%><td>
<td><%= abundance.sample.race %></td>
this because you said in the comments that abundances have a sample_id attribute, so abundance belongs_to a sample. of course this will work if abundance have the attribute of sample_id filled with a valid ID (an existing one sample) and of course, that it has a race value.
I've got two models - service_request_work_plan where service_request_work_plan has_many work_plan_tasks and work_plan_tasks belongs to service_request_work_plan. The linkage works, associations render properly and I have the code below in my show view for service_reqeust_work_plan, the goal of which is to show the work_plan_tasks in order. The show action works properly, but they are not showing in order (i.e. order_of_exeuction). What am I missing?
<table>
<tr>
<th>Order of Execution</th>
<th>Task</th>
<th>SLO</th>
<th>Task Instructions</th>
</tr>
<% #service_request_work_plan.work_plan_tasks.each do |work_plan_task| %>
<tr>
<td><%= work_plan_task.order_of_execution %></td>
<td><%= work_plan_task.task_name %></td>
<td><%= work_plan_task.task_slo %></td>
<td><%= work_plan_task.task_instructions %></td>
</tr>
<% end %>
</table>
class ServiceRequestWorkPlan < ActiveRecord::Base
attr_accessible :testing_company_id, :work_plan_name, :work_plan_comments
belongs_to :testing_company
has_many :work_plan_tasks
end
class WorkPlanTask < ActiveRecord::Base
attr_accessible :testing_company_id, :task_name, :task_instructions, :service_request_work_plan_id, :task_slo, :order_of_execution
belongs_to :testing_company
belongs_to :service_request_work_plan
end
def show
#service_request_work_plan = ServiceRequestWorkPlan.find(params[:id])
end
Add a .order(order_of_execution: :asc) for work_plan_tasks when querying the database from the controller. See http://guides.rubyonrails.org/active_record_querying.html#ordering
Controller:
def show
#service_request_work_plan = ServiceRequestWorkPlan.find(params[:id]).work_plan_tasks.order(order_of_execution: :asc)
end
Change this line:
<% #service_request_work_plan.work_plan_tasks.each do |work_plan_task| %>
into this line:
<% #service_request_work_plan.work_plan_tasks.all.order(:order_of_execution).each do |work_plan_task| %>
Default order is ascending, so the above line is complete. If you need descending order:
order(:order_of_execution => :desc)
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.
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