I currently am using this peice of code to loop through ordered items for a specific date
<tr>
<th>Item</th>
<th>Quantity</th>
</tr>
<% #demand.each do |d| %>
<% d.demand_items.each do |item| %>
<tr>
<td><%= item.item.name %></td>
<td><%= #item_count %></td>
</tr>
<% end %>
<% end %>
Currently if item 1 is ordered more than once, it shows up multiple times in the list. I just want it to show up once and have a number next to it to show how many are ordered. For example, if item_1 has a quantity of 5 in demand_1 and item_1 has a quantity of 10 in demand_2, the result should be:
item_1 .... 15
Thanks!
This should do it:
<tr>
<th>Item</th>
<th>Quantity</th>
</tr>
<% #demand.flat_map(&:demand_items).group_by(&:item).each do |item, demands| %>
<tr>
<td><%= item.name %></td>
<td><%= demands.map(&:quantity).inject(:+) %></td>
</tr>
<% end %>
Hope this helps!
Some explanation:
#demand.flat_map(&:demand_items)
# equivalent: (long version)
#demand.map{ |demand| demand.demand_items }.flatten
# retrieves all demand_items of each demand in the #demand list
# flatten the result (which is a double-dimension array)
demands.map(&:quantity)
# sends .quantity call to each element of the demands list
# and put it in an array (so this returns an array of quantity of each demand)
# equivalent: (long version)
demands.map{ |demand| demand.quantity }
demands.map(&:quantity).inject(:+)
# the inject(:+) will inject the method + (add) between each element of the array
# since the array is a list of quantities
# the inject(:+) sums each quantity of the list
I would try and get the uniq items and then count them while looping, something like:
(#demand.demand_items.sort.uniq).each do |d|
d.item_name
d.demand_items.count
end
This is untested pseudo code.
Related
I realize the heading is a little confusing but my problem is quite simple. I hae two models in my rails 5 app. User and Expense. Each expense belongs_to a user. I have an index page where all expenses are being listed. I can list the user IDs for each expense from the expenses table but I want to instead look up the name of the user (in column username) in the users table and display it with the expense instead. The view I have written is below. But it doesn't work.
<p id="notice"><%= notice %></p>
<h1>Teamjournals</h1>
<table style="padding: 2px; width: 50%" border="2px" align="center">
<thead>
<tr>
<td align="center"><%= link_to new_expense_path, :class =>"btn btn-success btn-wide" do%>Add New Expense<% end %></td>
</tr>
<tr>
<th>User</th>
<th>Expense Date</th>
<th>Currency</th>
<th>Expense Amount</th>
<th>Description</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody>
<% #expenses.each do |expense| %>
<tr>
<td><%= User.joins(:expense).where('expense.user_id = ?', #user.id) %></td>
<td><%= expense.expense_date %></td>
<td><%= expense.currency.currency %></td>
<td align="right"><%= expense.expense %></td>
<td><%= expense.description %></td>
</tr>
<% end %>
</tbody>
</table>
Ok so in your iteration over #expenses you have this line:
<%= User.joins(:expense).where('expense.user_id = ?', #user.id) %>
you can change it to this:
<% user = expense.user %>
Note that I'm using <% not <%= because I'm just trying to assign a variable, not print the output to html.
Then after defining user you can say <%= user.name %>.
You should read a bit more about active record associations, but here's a few side comments about the query you've shown
User.joins(:expense).where('expense.user_id = ?', #user.id)
In this case, you should use the method generated by belongs_to instead of writing a query. But in situations where you do want to write a custom query, you should only be using where when you want to get an array. In this case you're looking for a single record so you could use find_by. Furthermore, the joins you're doing here is unnecessary
# any of these works
user = User.where('id = ?', expense.user_id).first
user = User.where(id: expense.user_id).first
user = user.find_by(id: expense.user_id)
I am displaying a list of user in my table (no pagination). User contains id, name, age, status, location. I want to allow the list to be filtered by age or status. How do I do this in ruby rails ? I tried using filterrific but stuck since last two days here
Id name age status location
1 xz 22 single ca
2 yy 23 married ma
view
<table>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>status</th>
<th>location</th>
</tr>
<% items.each do |item| %>
<tr>
<td><%= item.id %></td>
<td><%= item.name %></td>
<td><%= item.age %></td>
<td><%= item.status %></td>
<td><%= item.lcation %></td>
</tr>
<% end %>
</table>
controller
def index
#items = User.all
end
I think, this cast can help you perfect without any gems.
I need to make sum of column.
I think problem is in class, in action where I take performance_reports.
I have associsations, so I get all reports related to user by next code:
#performance_reports = Array.new
current_user.websites.each do |website|
reports = website.performance_reports
reports.each do |report|
#performance_reports.push(report)
end
end
In my view I'm printig each report and in the bottom I want to print summary of column.
Here is view:
<% #performance_reports.each do |performance_report| %>
<tr>
<td><%= performance_report.impressions %></td>
<td><%= performance_report.clicks %></td>
<td><%= performance_report.conversions %></td>
</tr>
<% end %>
<tr>
<td><%= #performance_reports.count(:clicks)%></td>
<td><%= #performance_reports.count(:conversions)%></td>
</tr>
All - clicls, conversions - are integer type.
This code is printing, that summary is 0.
I have two questions: how to sum integers and how to sum not integer columns, which are not saving to database?
you can use the method sum for this
#performance_reports.sum(:clicks)
it will also sum not integer also as it will return non integer type, to convert it in integer call like this
#performance_reports.sum(:clicks).to_i
I have a loop in one of my views to display a table like so:
Each category object has 5 attributes called: level_1, level_2, level_3, level_4, level_5.
There will be an array with all the category objects. so there could be any amount of categories and no necessarily 3.
what would the best way to draw this up be? i have something like this at the moment but dont know how to select the relevant category level attribute in the 5.times loop.
<table border="0">
<tr>
<th>Maturity</th>
<% for category in #categories %>
<th><%= category.category_title %></th>
<% end %>
</tr>
<% 5.times do |i|%>
<% level = 5 - i %>
<tr>
<td>Level <%= level %> Maturity</td>
<% for category in #categories %>
<td><%= category.level_ #put in the level value here so it selects the relevant attraibute %></td>
<% end %>
</tr>
<% end %>
</table>
you need to change the rendering of level with this:
<% for category in #categories %>
<td><%= category.send("level_#{level}") %></td>
<% end %>
send is used to call a method on an object so that you can compose your method at runtime.
If you categories as variable no. then you shouldn't make it columns, but rows. And then levels will be you columns e.g.
<table>
<tr>
<th>Level 1</th>
<th>Level 2</th>
<th>Level 3</th>
<th>Level 4</th>
<th>Level 5</th>
<tr>
<% #category.each do |c| %>
<tr>
<td>#category.level_1<td>
<td>#category.level_2<td>
<td>#category.level_3<td>
<td>#category.level_4<td>
<td>#category.level_5<td>
<th>
<% end %>
Now in above code you may replace hard coded level_#{no} with iterations.
Hi i have a table where i list different values. Now i want to add a total values to my table that counts the values from each column and adds it to a column with the total value. How do I do this? And is it possible to do this in a simple way in the view, or am I bound to do it in the model? Thanks for any help!
<div class="chart-data">
<table>
<caption>Data</caption>
<thead>
<tr>
<% statistic.column_titles.each do |column| %>
<th><%= column %></th>
<% end %>
<th>Total</th>
</tr>
</thead>
<tbody>
<% statistic.rows.each do |row| %>
<tr>
<th scope="row"><%= row.title %></th>
<% row.data.each do |column| %>
<td><%= column %></td>
<% end %>
</tr>
<------- Here i want to have the code that sums the values from the columns above to form the 'Total'
<% end %>
</tbody>
</table>
</div>
The simple solution would be to maintain a variable that sums up the column, then use it for the total at the end.
class Row < ActiveRecord::Base
def total_spent
sum(:data)
end
end
<tbody>
<% #rows.each do |row| %>
<tr>
<th scope="row"><%= row.title %></th>
<% row.data.each do |column| %>
<td><%= column %></td>
<% end %>
</tr>
<td><% row.total_spent %></td>
<% end %>
</tbody>
Assuming of course that row is a model you could move this logic inside the row model. You may cache the page as well if you like.
If your data is the result of an ActiveRecord query against a database you could ask the database to do the work of summing all the columns. It would be one additional query but would run very quickly as that's something databases are designed to do.