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 6343791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:30:51+00:00 2026-05-24T20:30:51+00:00

I have some data inside this variable and I want to know how could

  • 0

I have some data inside this variable and I want to know how could I write an method to give me the sum of the items price.

Model names and their associations:

Order
- belongs_to :customer
- has_many :item_orders
- has_many :items, :through => :item_orders
- accepts_nested_attributes_for :item_orders, :allow_destroy => true, :reject_if => lambda { |a| a['item_id'].blank? }


Item
- has_many :item_orders, :dependent => :destroy
- has_many :orders, :through => :item_orders
- accepts_nested_attributes_for :item_orders, :allow_destroy => true

ItemOrder
- belongs_to :item
- belongs_to :order

doing:

<%= debug @order.items %> 

# returns me

- !ruby/object:Item
  attributes:
  id: 31
  title: Golden Ring 2
  price: 13445.0
  labour_cost: 500.0
  item_type_id: 10
  created_at: 2011-08-13 10:53:24.000000000Z
  updated_at: 2011-08-18 06:10:36.000000000Z
  photo: golden_ring.jpg
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false
- !ruby/object:Item
 attributes:
  id: 32
  title: Special Pendant
  price: 171.67
  labour_cost: 120.0
  item_type_id: 20
  created_at: 2011-08-13 11:09:43.000000000Z
  updated_at: 2011-08-14 06:03:02.000000000Z
  photo: prd_194_48cd9b21771dd.jpg
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false

So, I want to write a method in the Order model class to make possible writes something like this on the view.

 <%= @order.items.total_price %>

The total_price method will sum all the item prices and multiply by the quantity that contains in @order.items.

My doubt is: from inside the method, how could I have access to the items collection to perform the sum operation.

def total_price
  self.item_order.each do |i| { 
     result += i.quantity * i.items.price
  }
end

EDIT: I totally forgot to mention that I have to consider the quantity also to make this math.

  • 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-24T20:30:53+00:00Added an answer on May 24, 2026 at 8:30 pm

    You have two possible approaches.

    1) Use ActiveRecord sum. This is efficient as the SUM operation is performed in the DB. When you are dealing with 100s of rows this is the way to go:

    items.sum(:price)  # executes select sum(items.price) 
                       #          from items where items.order_id = some_id
    

    2) Use Enumeration sum. Here you are operating on an existing array.

    items.to_a.sum(&:price)
    

    I use this approach when my has_many association list size is small( < 10) and already eager loaded.

    Edit 1: Based on additional requirement

    Using ActiveRecord:

    item_orders.sum("item_orders.quantity * item.price", :join => :item)
    

    Using Enumeration

    item_orders.all(:include => :item).sum {|io| io.quantity * io.item.price }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data. I want to go through that data and change cells
I have some data that I want to store somewhere in my Rails app
I have some data stored as ArrayList . And when I want to backup
I have a web task for counting some data inside DB. I must count
I have some data formatted into a table. I want to setup an onclick
I have some data with messed-up accented characters. For example in the data we
I have some data grouped in a table by a certain criteria, and for
I have some data of the form Key ID Link 1 MASTER 123 2
I have some data files to import into a database with some unique delimiters:
I have some code which needs to ensure some data is in a mysql

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.