Deploying rails app on heroku, not displaying SQL query - ruby-on-rails

can you help me with this problem?
I cannot execute "ActiveRecord::Base.connection.execute("SQL")" on production version (deployed on Heroku), although it works locally.
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Games</th>
<th>Wins</th>
<th>Win Ratio</th>
</tr>
</thead>
<tbody>
<% ActiveRecord::Base.connection.execute("
select * from players").each_with_index do |performer, i| %>
<tr>
<td><%= (i+1) %> </td>
<td><%= performer[0] %> </td>
<td><%= performer[1] %></td>
<td><%= performer[2] %></td>
<th><%= performer[3] %> <%='%'%></th>
</tr>
<% end %>
</tbody>
DEVELOPMENT
PRODUCTION
HEROKU LOGS

This should be a comment (sorry no rep yet)
Since you are calling all players why not do something like
<% #players.each do |p| %>
<td><%= (i+1) %> </td>
<td><%= p.name %> </td>
<td><%= p.games %></td>
<td><%= p.wins %></td>
<% percent = p.wins / p.games %>
<% percent = percent * 100 %>
<% percent = number_with_precision(percent, :precision => 0) %>
<th><%= percent %> %</th>
<% end %>
In controller do something like #players = Player.all
This will remove the need for any SQL statement in your view, removing the problem of having pg and sql

Related

I'm trying to complete a table iterating in groups of data

I know this code is wrong, but I want to know which is the correct form to do it, I'm trying to iterate the groups, and each group have 8 teams
<table class="table">
<thead>
<tr>
<% #groups.each do |group| %>
<th><%= group.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #groups.each_slice(4) do |group_a, group_b, group_c, group_d| %>
<% group_a.teams.each do |team_a|, group_b.teams.each do |team_b|, group_c.teams.each do |team_c|, group_d.teams.each do |team_d| %>
<tr>
<td><%= team_a.name %></td>
<td><%= team_b.name %></td>
<td><%= team_c.name %></td>
<td><%= team_d.name %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
Solution actually depends on the table structure you want to see, which you haven't clarified in the question. Here is an attempt; see how this ends up:
<table class="table">
<tbody>
<thead>
<th>Team#1</th>
<th>Team#2</th>
<th>Team#3</th>
<th>Team#4</th>
</thead>
<% #groups.each do |group| %>
<tr>
<th colspan='4'><%= group.name %></th>
</tr>
<tr>
<% group.teams.each do |team| %>
<td><%= team.name %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Thanks #JagdeepSingh for your help, after try and try this is what im looking for bro:
`<table class="table">
<thead class="thead-dark">
<tr>
<% #groups.each do |group| %>
<th><%= group.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #groups.each_slice(4) do |group_a, group_b, group_c, group_d| %>
<% for x in 0..7 do %>
<tr>
<td><%= group_a.teams[x].name %></td>
<td><%= group_b.teams[x].name %></td>
<td><%= group_c.teams[x].name %></td>
<td><%= group_d.teams[x].name %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>`

has_many loop undefined method

I have a has_many association in which I want to display in a table. Each Agent can make up to 3 sales, but I want to list them in the same row as the Agent.
undefined method `each' for nil:NilClass on the <% sales.each do |s| %> line.
I'm getting the above error, event though there is defiantly data present for each column
<table>
<tr>
<td>Agent</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
</tr>
<% #agent.sales.each do |agent, sales| %>
<tr>
<td><% agent.name %></td>
<% sales.each do |s| %>
<td><%= s.date %></td>
<td><%= s.product %></td>
<td><%= s.price %></td>
<% end %>
</tr>
<% end %>
</table>
To start, since #agent.sales is an ActiveRecord_Associations_CollectionProxy, you can think of this object more like an array, rather than a hash.
How you initially wrote your loop it appears you were treating it like a hash:
#agent.sales.each do |agent, sale| > this syntax is saying for each key-value pair in the hash, do something with the key (agent) and value (sale).
The second time you try to run a loop, you're running it on the "value" part of the iterator, which does not exist, hence the error on the line sales.each do |sale|
Really, you are very close but are overthinking it. Just drop your first iterator and keep the rest:
<tr>
<td><% #agent.name %></td>
<% #agent.sales.each do |s| %>
<td><%= s.date %></td>
<td><%= s.product %></td>
<td><%= s.price %></td>
<% end %>
</tr>
By the way, you can apply the same principle and refactor your headers too:
<tr>
<td>Name</td>
<% 3.times do |n| %>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<% end %>
</tr>
This assumes that your constraint is 3 max sales.
This should work:
<table>
<tr>
<td>Agent</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
</tr>
<% #agent.sales.each do |sale| %>
<tr>
<td><% #agent.name %></td>
<td><%= sale.date %></td>
<td><%= sale.product %></td>
<td><%= sale.price %></td>
</tr>
<% end %>
</table>

Rails loop and rowspan

I'm having issues with rowspan in a loop in my form using conditional statements. My first Rowspan 2 is outside the loop and works fine, but my second Rowspan 2 is inside the conditional, therefore it does not work as it should. Is there a method to resolve this?
This is what I'm trying to achieve
Form
<table>
<th>Header 1</th>
<th>Header 2</th>
<th>Description</th>
<th>Phase</th>
<tr>
<td rowspan="4">Rowspan 4</td>
<td rowspan="2">Rowspan 2</td>
<% Identity.all.each do |identity| %>
<%= form.fields_for :indicators, form.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
<%= ff.hidden_field :id %>
<%= ff.hidden_field :identity_id %>
<% if ff.object.identity.number <= 1.4 %>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
</tr>
<tr>
<% elsif ff.object.identity.number > 1.4 %>
<td rowspan="2">Rowspan 2</td>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
<% end %>
</tr>
<% end %>
<% end %>
</table>
This is definitely doable, but I think it's not working for you based on how you're partitioning the data.
Specifically, it seems like you're closing trs in weird places, as well as you create one rowspan outside the loop.
A more robust solution would likely partition the data into rowspan=X blocks (maybe by using group_by, in_groups_of, chunk)
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Description</th>
<th>Phase</th>
</tr>
<tr>
<td rowspan="4">Rowspan 4</td>
<% Identity.all.each_with_index do |identity, index| %>
<% if index.positive? %>
<tr> #emit a tr in every row but the first
<% end %>
<% if index.even? %>
<td rowspan="2">Rowspan 2</td>
<% end %>
<%= form.fields_for :indicators, form.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
<%= ff.hidden_field :id %>
<%= ff.hidden_field :identity_id %>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
</tr> #Close a tr in every row
<% end %>
<% end %>
</table>
I haven't tested this personally, but I think something closer to this format would work. It would also be useful to see the html output you're actually getting to debug.

How to create a non-static HTML table header and rows

I have two arrays, and I'd like to create a table with dynamic header cells (from the first array called subjects) and iteratively add contents (from the second array called examscores) in table rows with respect to the table header value.
Desired outcome is (fiddle):
The erb code is:
<table width="100%" border="1">
<thead>
<tr>
<th rowspan="2" scope="col">NAME</th>
<th colspan="<%= #subjects_by_class.size %>" scope="col">Subjects/Scores</th>
<th rowspan="2" scope="col">Total</th>
<th rowspan="2" scope="col">Average</th>
</tr>
<tr>
<% #subjects_by_class.each do |s| %>
<th> <%= s.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #examscore.each do |ex| %>
<tr>
<td><%= get_student_name_by_id(ex.student_id) %></td>
<% #subjects_by_class.each do |ss| %>
<% #examscore.each do |ii| %>
<% if ss.id == ex.subject_id %>
<td> <%= i.total %> </td>
<% break %>
<% end %>
<% end %>
<% end %>
<td><%= sum_student_totalscore(ex.student_id, year_id) %> </td>
<td><%= avg_student_totalscore(ex.student_id, year_id) %></td>
</tr>
<% end %>
</tbody>
</table>
The output I get is (fiddle):
A new tr is created under Maths subject instead of a new td for Arts subject, and this results in Average td being distorted.
Any insight will be greatly appreciated.
Well, just look at this section of your code:
<% #examscore.each do |ex| %>
<tr>
You create a new line for each #examscore, and you have 4 of those (1 per user/subject, so you end up with 4 lines of course).
Your tbody should be something like this:
<tbody>
<% #students.each do |student| %>
<tr>
<td><%= student.name %></td>
<% #subjects_by_class.each do |subject| %>
<% #examscore.each do |score| %>
<% if score.subject_id == subject.id && score.student_id == student.id %>
<td><%= score.total %></td>
<% break %>
<% end %>
<% end %>
<% end %>
<td><%= sum_student_totalscore(student.id, year_id) %> </td>
<td><%= avg_student_totalscore(student.id, year_id) %></td>
</tr>
<% end %>
</tbody>
It's a bit strange that you only care about the year in your totals
Also you could improve things a little bit by having a method in your Student class that returns an array of scores for a given year/list of subjects
# Returns an array of scores for the given year
def scores(year, subject_ids)
subject_ids.map do |subject_id|
# find score for year & the given subject_id
end
end
This way your body would become
<tbody>
<% #students.each do |student| %>
<tr>
<td><%= student.name %></td>
<% #scores = student.scores(year_id, #subjects_by_class) %>
<% #scores.each do |score| %>
<td><%= score.total %></td>
<% end %>
<% scores_total = #scores.sum(&:total) %>
<td><%= scores_total %> </td>
<td><%= scores_total / #scores.size.to_f %></td>
</tr>
<% end %>
</tbody>
which I found more clear, but it could be further improved with decorators for instance.

search the contents of a table in rails

I want to place a searchbox on my table to filter it according to eventtitle.
Any ideas how I can go about this? I've tried two tutorials but it didn't work. I've also tried the filterrific gem but it didnt work either.
So far I just have this loading my table in the controller
#newevents = Event.order("id").page(params[:page]).per(50)
And this in my view
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>EventName</th>
<th>Date</th>
<th>Time</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<% #newevents.each do |ne| %>
<% if ne.eventready.blank?%>
<tr>
<td><%= ne.id %></td>
<td><%= ne.eventname %></td>
<td><%= ne.date %></td>
<td><%= ne.time %></td>
<td><%= link_to "Edit Event", edit_event_path(ne.id), class: "btn btn-info" %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= paginate #newevents %>

Resources