I have multiple variables which I'm having issues lining up in a table. I have each grouping by year, but when the table is generated if there is only one results for one of the columns it puts it in the first row, instead of the corresponding year.
Here is an image of what it's doing. The Drylands column should be in the year 2006.
View
<% #all.zip(#irrigated, #semi, #dryland) do |a, b, c, d| %>
<tr>
<td><%= a.year %></td>
<td><% if a.nil? %>0<% else %><%= a.trial_id %><% end %></td>
<td><% if a.nil? %>0<% else %><%= "%.2f" % (a.lint/227) %><% end %></td>
<td><% if b.nil? %>0<% else %><%= b.trial_id %><% end %></td>
<td><% if b.nil? %>0<% else %><%= "%.2f" % (b.lint/227) %><% end %></td>
<td><% if c.nil? %>0<% else %><%= c.trial_id %><% end %></td>
<td><% if c.nil? %>0<% else %><%= "%.2f" % (c.lint/227) %><% end %></td>
<td><% if d.nil? %>0<% else %><%= d.trial_id %><% end %></td>
<td><% if d.nil? %>0<% else %><%= "%.2f" % (d.lint/227) %><% end %></td>
</tr>
<% end %>
Controller
#variety = Variety.where(variety_id: params[:variety_select]).group('variety_name')
#all = Result.where(variety_id: params[:variety_select]).group('results.year').where('results.lint > 0').select('AVG(results.lint) AS lint, results.year as year, COUNT(results.trial_id) AS trial_id').joins(:trial)
#irrigated = Result.where(variety_id: params[:variety_select]).group('results.year').where('results.lint > 0').select('AVG(results.lint) AS lint, results.year as year, COUNT(results.trial_id) AS trial_id').joins(:trial).where('trials.irrigated = ?', '1')
#dryland = Result.where(variety_id: params[:variety_select]).group('results.year').where('results.lint > 0').select('AVG(results.lint) AS lint, results.year as year, COUNT(results.trial_id) AS trial_id').joins(:trial).where('trials.irrigated = ?', '0')
#semi = Result.where(variety_id: params[:variety_select]).group('results.year').where('results.lint > 0').select('AVG(results.lint) AS lint, results.year as year, COUNT(results.trial_id) AS trial_id').joins(:trial).where('trials.irrigated = ?', '2')
I first would check what the queries actually return. Do you have records in Drylands for years 2004 and 2005? It doesn't seem so.
Maybe you can do this
a is never nil, otherwise a.year would crash, so no need for a.nil?
for the other values (b, c, d) you can check if !b.nil? and b.year == a.year ...
This should only print values in the respective year rows
Related
I'm pretty new to Ruby on Rails and Ruby in general but I'm trying to make a small website with simple database in Ruby on Rails.
At the moment I have the html.erb pages to show, add and edit records.
The next thing i wanted to do is the action that redirects user to a page with more info about the record he clicked in the record table.
I can't really think of any way to do this.
Any help would be really appriciated.
p.s. Sorry for any mistakes in my English - it's not my first language and im still learning!
Here is my html code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tablecontainer">
<table class="table table-bordered table-condensed">
<tr class="success">
<td><b>Nazwa</b></td>
<td><b>Obrażenia</b></td>
<td><b>Typ</b></td>
<td><b>Waga</b></td>
<td><b>Zasięg</b></td>
<td><b>Szybkość</b></td>
<td><b>Rzadkość</b></td>
<td><b>Opcje</b></td>
</tr>
<% #biala.each do |b| %>
<tr>
<td><%= b.nazwa %></td>
<td><%= b.obrazenia %>%</td>
<td><%= b.typ %></td>
<td><%= b.waga %></td>
<td><%= b.zasieg %></td>
<td><%= b.szybkosc %></td>
<td><%= b.rzadkosc %></td>
<td><%= link_to '', {id: b.id, action: 'db_wiecejbiala'}, class: "glyphicon glyphicon-info-sign" %><%= link_to '', {id: b.id, action: 'db_edytujbiala'}, class: "glyphicon glyphicon-pencil" %> <%= link_to '', {id: b.id, action: 'usunbiala'}, data: {confirm: 'Jesteś tego pewien?'}, class: "glyphicon glyphicon-remove" %></td>
</tr>
<% end %>
</table>
And here is the controller:
class BazaController < ApplicationController
def db_bronbiala
#biala = BronBiala.all
#iloscbiala = BronBiala.count
end
def db_dodajbiala
#nowybiala = BronBiala.new
end
def utworzbiala
#nowybiala = BronBiala.new(parametrybiala)
if #nowybiala.save
redirect_to(action: 'db_bronbiala')
else
render('db_dodajbiala')
end
end
def parametrybiala
params.require(:bron_biala).permit(:nazwa, :obrazenia, :typ, :waga, :zasieg, :szybkosc, :rzadkosc, :zalety, :wady, :ciekawostki, :opis)
end
def usunbiala
usuwaniebiala = BronBiala.find(params[:id]).destroy
#biala = BronBiala.all
render('db_bronbiala')
end
def db_edytujbiala
#biala = BronBiala.all
#edytowanabiala = BronBiala.find(params[:id])
end
def aktualizujbiala
#biala = BronBiala.all
#edytowanabiala = BronBiala.find(params[:id])
if #edytowanabiala.update_attributes(parametrybiala)
redirect_to(action: 'db_bronbiala')
else
render('db_edytujbiala')
end
end
def db_wiecejbiala
#biala = BronBiala.all
#bialawiecej = BronBiala.find(params[:id])
end
end
And the db_bialawiecej code:
<div class="content">
<h2>Lista:</h2>
<div class="tablecontainer">
<table class="table table-bordered table-condensed">
<tr class="success">
<td><b>Nazwa</b></td>
<td><b>Obrażenia</b></td>
<td><b>Typ</b></td>
<td><b>Waga</b></td>
<td><b>Zasięg</b></td>
<td><b>Szybkość</b></td>
<td><b>Rzadkość</b></td>
</tr>
<% #bialawiecej.id do |b| %>
<tr>
<td><%= b.nazwa %></td>
<td><%= b.obrazenia %>%</td>
<td><%= b.typ %></td>
<td><%= b.waga %></td>
<td><%= b.zasieg %></td>
<td><%= b.szybkosc %></td>
<td><%= b.rzadkosc %></td>
</tr>
<% end %>
</div>
</div>
On click send id of clicked item (GET). you will have link similar to : localhost:3000/desired_model/5
then in action do #desired_model = DesiredModel.find(params[:id])
redirect user to desired show page.
Show data.
Next time please provide some code :)
I don't know how appropriate title for this question.
Well, This is my problem
I have index action on homes_controller.rb looks like :
def index
#r = 2
#datas = [[1, 10], [2, 10], [3, 15], [4, 20], [5, 70], [6, 180], [7, 250], [8, 270], [9, 230], [10, 40], [11, 0], [12, 10]]
end
I want to result from above like this:
And on view looks like :
<table>
<thead>
<tr>
<th>Month</th>
<th>Forecast</th>
<th>Order</th>
<th>Begining Inventory</th>
<th>Ending Inventory</th>
</tr>
</thead>
<tbody>
<% ei = 0 %>
<% #datas.each_with_index do |d, index| %>
<tr>
<td><%= d[0] %></td>
<td><%= d[1] %></td>
<td><% if d[1] > bi %>
<% #datas.transpose.at(1).slice(index, #r).inject do |sum, s| -%>
<%= #biu = sum + s %>
<% end %>
<% end %>
</td>
<td><%= #bi = ei + #biu %></td>
<td><%= #ei = #bi - d[1] %></td>
</tr>
<% #biu = 0 %>
<% ei = #ei %>
<% end %>
</tbody>
</table>
State :
When the forecast d[1] is bigger than ei the calculation order #biu is 2 (#r) next of forecast.
Begining Inventory #bi is calculation from ei plus order #biu
Ending Inventory #ei is calculation from #bisubtracted with forecast d[1]
And then #ei put to ei
But the result like this :
I want 12th months must have : Order = 10, Begining invetory = 10, and Ending Inventory = 0
Your problem is here:
<% #datas.transpose.at(1).slice(index, #r).inject do |sum, s| -%>
<%= #biu = sum + s %>
<% end %>
When an array has only one item, inject without a starting value does not run the block at all:
puts [1,2].inject { |sum, s| puts 'was here!'; sum + s }
# was here!
# 3
puts [1].inject { |sum, s| puts 'was here!'; sum + s }
# 1
puts [1].inject(0) { |sum, s| puts 'was here!'; sum + s }
# was here!
# 1
If you add a starting value 0 to your code, though, #biu will be printed twice. To avoid that you need to move the assignment outside of the block:
<% #biu = #datas.transpose.at(1).slice(index, #r).inject do |sum, s| -%>
<% sum + s %>
<% end %>
<%= #biu %>
Or, more succinctly:
<%= #biu = #datas.transpose.at(1).slice(index, #r).inject(:+) %>
been teaching myself how to build a simple ruby/rails site. trying now to figure out the caveats for data manipulation. group is working, how do i "sum" all the columns?
index.html.erb
<table>
<tr>
<th>Site</th>
<th><7days</th>
<th>>7days</th>
<th>>30days</th>
<th>>90days</th>
<th>>180days</th>
<th>>365days</th>
</tr>
<tr>
<% #gdcomets.each do |dcomet| %>
<td><%= dcomet.site %></td>
<td><%= #sum1 %></td>
<td><%= #sum2 %></td>
<td><%= #sum3 %></td>
<td><%= #sum4 %></td>
<td><%= #sum5 %></td>
<td><%= #sum6 %></td>
</tr>
<% end %>
</table>
dcomets_controller
class DcometsController < ApplicationController
# GET /dcomets
# GET /dcomets.json
def index
#dcomets = Dcomet.all
#gdcomets = Dcomet.group(:site)
#sum1 = #gdcomets.map(&:ls7day).sum
#sum2 = #gdcomets.map(&:gt7day).sum
#sum3 = #gdcomets.map(&:gt30day).sum
#sum4 = #gdcomets.map(&:gt90day).sum
#sum5 = #gdcomets.map(&:gt180day).sum
#sum6 = #gdcomets.map(&:gt365day).sum
respond_to do |format|
format.html # index.html.erb
format.json { render json: #gdcomets }
end
end
end
#sum = #gdcomets.map(&:data).sum
:data being the name of the column you want to sum.
You'll get better performance letting SQL do the donkey work of summing everything:
Dcomet.group(:site).sum(:data)
Where :data is the name of column you want to sum. That will return a hash with the keys being the site values, and the values the corresponding sum for that site
I did a query in MySql but is working in Rails and mysql2 gem.
Here is the information:
http://sqlfiddle.com/#!2/9adb8/6
The query is working fine without problems and showing this result:
UNIT V1 A1 N1 V2 A2 N2 V3 A3 N3 V4 A4 N4 V5 A5 N5
LIFE 2 0 0 1 2 0 0 0 0 0 0 0 0 0 0
ROB 0 1 0 0 1 2 0 0 0 0 0 0 0 0 0
-Installed mysql2 gem for rails 2.3.8
gem install mysql2 -v0.2.6
-Created the controller:
class PolicyController < ApplicationController
def index
#result = ActiveRecord::Base.connection.execute("select distinct #sql := concat('SELECT pb.name as unit,',group_concat(concat('SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),' FROM cia_ensures ce LEFT JOIN policies p on ce.id = p.cia_ensure_id INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id AND ca.id=1 Group by p.policy_business_unit_id') from cia_ensures where id in(1,2,3,4,5);")
#result2 = ActiveRecord::Base.connection.execute("prepare stmt from #sql;")
#result3 = ActiveRecord::Base.connection.execute("execute stmt;")
end
end
Here is the log:
SQL (0.9ms) select distinct #sql := concat('SELECT pb.name as unit,',group_concat(concat('SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),' FROM cia_ensures ce LEFT JOIN policies p on ce.id = p.cia_ensure_id INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id AND ca.id=1 Group by p.policy_business_unit_id') from cia_ensures where id in(1,2,3,4,5);
SQL (0.9ms) prepare stmt from #sql;
SQL (0.2ms) execute stmt;
Here is the view (is working fine and without problems but seems to be too long write several times the same code)
<table>
<% #result3.each do |policy| %>
<tr>
<td><%= policy[0] %></td>
<td><%= policy[1] %></td>
<td><%= policy[2] %></td>
<td><%= policy[3] %></td>
<td><%= policy[4] %></td>
<td><%= policy[5] %></td>
...
</tr>
<%end%>
</table>
I tried to use inspect but it shows all the information in one td and not on each td:
<% #result3.each do |policy| %>
<tr>
<td align="center"><%= policy.inspect %></td>
</tr>
<%end%>
How can I do to show all this without writing lots of lines?
Is it possible to make this in one line? without writing <%= policy[#NUMBER] %>
Please somebody can help me with this?
I will really appreciate it.
Hmmm, might have missunderstood your question since it looks really simple, are you trying to shorten this?
Change:
<td><%= policy[0] %></td>
<td><%= policy[1] %></td>
<td><%= policy[2] %></td>
<td><%= policy[3] %></td>
<td><%= policy[4] %></td>
<td><%= policy[5] %></td>
To:
<% policy.each do |p| %>
<td><%= p %></td>
<% end %>
Do this
<% #result3.each do |policy| %>
<tr>
<% policy.each { |p| raw "<td>#{p}</td>" } %>
</tr>
<%end%>
Just iterate over each collection in each policy object.
<table>
<% #result3.each do |policy| %>
<tr>
<%policy.each do |item| %>
<td><%= item %></td>
<%end%>
</tr>
<%end%>
</table>
I am trying to make a table with ten values across on each row starting at 1 and going to 100.
My Ruby code looks like this:
<table border="1">
<% (1..100).each do |i|
d3 = (i % 3 == 0)
d5 = (i % 5 == 0)
i = "<b>#{i}</b>" if d5
i = "<i>#{i}</i>" if d3 %>
<tr>
<td><%= i %></td>
</tr>
<% end %>
</table>
How would I put this in an HTML table in a 10 X 10?
Using ERB:
<table>
<%10.times do |row|%>
<tr>
<%10.times do |col|%>
<td><%=
i = row*10+col+1
if i%5==0
"<b>#{i}</b>"
elsif i%3==0
"<i>#{i}</i>"
else
i
end
%></td>
<%end%>
</tr>
<%end%>
</table>
Using Haml:
%table
- 10.times do |row|
%tr
- 10.times do |col|
%td
- i = row*10+col+1
= i%5==0 ? "<b>#{i}</b>" : i%3==0 ? "<i>#{i}</i>" : i
<table border="1">
<% (1..100).each do |i|
d3 = (i % 3 == 0)
d5 = (i % 5 == 0)
s = "#{i}"
s = "<b>#{i}</b>" if d5
s = "<i>#{i}</i>" if d3 %>
<% if i % 10 == 1 %><tr><% end %>
<td><%= s %></td>
<% if i % 10 == 0 %></tr><% end %>
<% end %>
</table>
Basically, you want to start a table row before elements 1, 11, 21, etc. and end a row after elements 10, 20, 30, etc.