I currently have a Order and image model which look like this:
class Order < ActiveRecord::Base
has_many :images
end
class Image < ActiveRecord::Base
belongs_to :order
end
Now i’d like to do the following:
<% @order.images.each do |image| %>
<%= image.format %> x
<%= image.amount %>
<%= number_to_currency(image.price) %><br/>
<% end %>
This prints out this:
1x 30x30 € 1,00
1x 12x12 € 2,10
3x 30x30 € 3,00
4x 12x12 € 8,40
I’d like to combine this by image format, and sum the amount and price. But I’ve tried several methods but none seem to work because the @order.images isn’t a simple array but an Image object. This is what I would like to achieve:
5x 12x12 € 10,50
4x 30x30 € 4,00
You could try using
group_by:Should do the trick.
imagesis an array of objects with equal values offormat.