Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:57:26+00:00 2026-05-28T01:57:26+00:00

I currently have a Order and image model which look like this: class Order

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:57:27+00:00Added an answer on May 28, 2026 at 1:57 am

    You could try using group_by:

    @order.images.group_by(&:format).each do |format, images|
      <%= "#{images.length}x #{format} #{number_to_currency(images.sum(&:price))" %>
    end
    

    Should do the trick. images is an array of objects with equal values of format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have this query set-up: SELECT topic.content_id, topic.title, image.location FROM mps_contents AS topic
I currently have this query set-up: SELECT topic.content_id, topic.title, image.location FROM mps_contents AS topic
I currently have a class and I'm trying to create an easy GUI to
I have a 'Complex' model which hasMany Unit. My model associations are correct, everything
I have an object that looks like this when outputted via a print_r Array
I have an image generator which would benefit from running in threads. I am
I'm currently writing a client-side javascript app which performs image manipulation. Some of the
I have the following CSS: body { margin-left:0px; margin-right:0px; margin-bottom:0px; margin-top:0px; background-image: url(../images/tile.png); background-repeat:
I have recently begin creating an image editing tool which will cater to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.