I'm trying to highlight the last entry inside a loop. But the following code assigns id="highlight" to every table row :(
<% #sales.order("created_at desc").each do |sale| %>
<tr <% if sale.created_at = Sale.last %>id="highlight"<% end%> >
<td><%= sale.user.name %></td>
<td><%= sale.product %></td>
....
Any ideas? Thank you guys!
if sale.created_at = Sale.last
should be
if sale.created_at == Sale.last.created_at
Alternatively, I guess the following would be better, since it eliminates an additional query Sale.last:
<% #sales.order("created_at desc").each_with_index do |sale, idx| %>
<tr <% if idx == #sales.length - 1 %>id="highlight"<% end%> >
<td><%= sale.user.name %></td>
<td><%= sale.product %></td>
....
HTH
To get the number of rows (without an additional db query), you can use:
#sales.length
Then use each_with_index for your loop:
<% #sales.order("created_at desc").each_with_index do |sale, i| %>
<tr <% if #sales.length == i + 1 %>id="highlight"<% end%> >
<td><%= sale.user.name %></td>
<td><%= sale.product %></td>
</tr>
<% end %>
Check the way you are using the = sign.
Note that equality(==) is different from assignment(=).
What you need is the equality sign(==) here. so:
<% if sale.created_at == Sale.last %>id="highlight"<% end%>
as opposed to:
<% if sale.created_at = Sale.last %>id="highlight"<% end%>
which you are doing.
Related
I created this code that pulls the information I need.
def index
#votes = Vote.all
#originalitysum = Vote.group(:widget_id).sum(:originality)
end
It returns a hash:
{188=>5, 160=>2}
I now need to match the key to the widget_id and return the value. I.E:
If the widget_id is 188 return 5.
<% #votes.group(:widget_id).each do |vote| %>
<tr>
<td><%= vote.widget.name %></td>
<td><%= vote.widget.store %></td>
<td><%= %></td> <!-- This needs to be the total Originality -->
<td><%= vote.interest %></td>
<td><%= vote.rating %></td>
</tr>
<% end %>
I'm open to changing this if some other way makes more sense.
You can get the originality sum with #originalitysum[vote.widget.id]
Figured it out.
Controller
def index
#votes = Vote.all
#originalitysum = Vote.select([:widget_id, :originality]).group('widget_id').sum(:originality)
#votecount = Vote.group(:widget_id).count(:originality)
#votes = Vote.select(:widget_id,
"SUM(originality) as originality_sum",
"SUM(interest) as interest_sum",
"SUM(rating) as rating_sum").group(:widget_id).order("rating_sum DESC")
end
The view
<% #votes.group(:burger_id).each do |vote| %>
<tr>
<td><%= vote.widget.name %></td>
<td><%= vote.widget.store %></td>
<td><%= vote.originality_sum %></td>
<td><%= vote.interest_sum %></td>
<td><%= vote.rating_sum %></td>
</tr>
<% end %>
Thanks to this answer in this link, I was able to parse it together.
Group by a column and then get the sum of another column in Rails
The added bonus is that it allowed me to easily sum the other columns as well.
Hi chaps and chappettes.
<tbody>
<% #orders_outstanding.limit(5).each do |order| %>
<% if order.completed_at.blank? && order.due_date.present? %>
<tr>
<td><%= order.order_number %></td>
<td><%= order.customer %></td>
<td><%= order.printer %></td>
<td><%= order.quantity %></td>
<td><%= order.due_date %></td>
</tr>
<% end %>
<% end %>
</tbody>
I'm using this little bit of code to display my next five orders due to ship. It's showing up in my development environment preview (puma/sqlite) but not on heroku (postgres). Is there any reason heroku doesn't like that formatting?
Thanks
I would put the conditions in the controller to make sure you have 5 that match your conditions:
#orders_outstanding = Order.where(completed_at: nil).where.not(due_date: nil).order("due_date")
Rails 3.2
I have the following in my view:
<% #organizations.each do |o| %>
<% organization = Organization.find(o.organization_id) %>
<% if organization == nil %>
<% next %>
<% end %>
<tr>
<td><%= o.org_name %></td>
<td><%= number_to_currency(o.revenue, :precision => 0) %></td>
<td><%= o.rank %></td>
</tr>
<% end %>
I was under the impression that if organization is not found, execution would jump to the next record. Instead, I am getting the "something went wrong".
Looking through the log file, one of the organization_id is not found in the organizations table, and that's what's triggering the error.
Why isn't execution jumping to the next record?
Temporary Solution: I changed
<% organization = Organization.find(o.organization_id) %>
To:
<% organization = Organization.find_by_id(o.organization_id) %>
and that gives me a nil when it does not find anything. If it's nil, the execution skips to the next record
According to http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find, Organization.find(o.organization_id) will raise an ActiveRecord::RecordNotFound for the id that does not exist in the table.
Addendum:
Try something like the following:
<% ids = #organizations.map(&:organization_id) %>
<% organizations = Organization.where(id: ids) %>
<% organizations.each do |o| %>
<tr>
<td><%= o.org_name %></td>
<td><%= number_to_currency(o.revenue, :precision => 0) %></td>
<td><%= o.rank %></td>
</tr>
<% end %>
With this method, you'll fetch all the organizations with 1 query from the DB as opposed to 1 for every organization.
I am having trouble figuring out how to shuffle table values in a view. I have a table in my view with a left and a right column, and would like to shuffle only the right column.
show.html.erb
<table>
<% #items.each do |item| %>
<tr>
<td><%= item.left %><td>
<td><%= item.right %><td>
</tr>
<% end %>
</table>
The "left" and "right" share the same primary id in the database. Any suggestions about how to shuffle only one side? Thanks!
You can use shuffle, do this way
<% shuffled_items = #items.shuffle %>
<% #items.each_with_index do |item, index| %>
<tr>
<td><%= item.left %><td>
<td><%= shuffled_items[index].right %><td>
</tr>
<% end %>
For details read this documentation http://ruby-doc.org/core-1.9.3/Array.html#method-i-shuffle
I think the simplest way is to have 2 arrays.
#items_left and #items_right
eg:
items = Item.a_scope
#items_left = items
#items_right = items.pluck(:right).shuffle #if you are on > rails 3.2
# #items_left = items.pluck(:left) #if only that attribute is needed
so you can use it as follows
<table>
<% #items_left.each_with_index do |item, i| %>
<tr>
<td><%= item.left %><td>
<td><%= #items_right[i] %><td>
</tr>
<% end %>
</table>
I am using pagination for my index page where I list all the users information like name, email etc. In the table I want to display the serial number in the order [1,2,3...]. If I user the user_id and if I delete the user the number will be missing out of sequence. I use the following code in my view
<% #user.each_with_index do |d, i| %>
<tr>
<td><%= i+1 %></td>
<% if d.profile.present? %>
<td><%= link_to d.profile.first_name+ " "+d.profile.last_name, posts_individualpostlink_path(:id => d.id) %> </td>
<% else %>
<td><%= "No Profile" %></td>
<% end %>
<td><%= d.email %></td>
<% if d.profile.present? %>
<td><%= d.profile.date_of_birth %> </td>
<% else %>
<td><%= "No Profile" %></td>
<% end %>
</tr>
<% end %>
</table>
<%= will_paginate #user %>
when I am going to the second page again the serial number starts with [1,2,....]. Per Page if i am giving 10 users, the second page should show [11, 12, 13,..... in the table.
Can anyone help me to do this. Thanks
Try with
<%
count = ((params[:page] || 1).to_i - 1) * 10
#user.each_with_index do |d, i| %>
<tr>
<td><%= count + i %></td>
Before answering question, small emotional note: stop using single letter variables in Your code. It makes it completely unreadable.
Why not use <% #user.each_with_index do |user, idx| %> ? Now in You code block it's easy to understand that You always refer to user.
Now the answer. Will paginate add page parameter to the paging links. So in You controller You should be able to do this:
#page = params[:page] || 1
After that use it to calculate correct number in Your view:
<td><%= (#page - 1) * number_of_items_on_page + i+1 %></td>