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

  • Home
  • SEARCH
  • 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 8268847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:01:50+00:00 2026-06-08T06:01:50+00:00

Hi im trying to sort output data without the controller in the layout, im

  • 0

Hi im trying to sort output data without the controller in the layout, im using devise, formtastic and some other gems to make the login and user interface

the problem is that i have 3 tables Products, Carted_products and Users, and i want to show the Carted_products by Product_id in the view, i got already it to show them by record in the table but i want it to SORT them example:

I have 3 different orders, 2 of them have the same product_id, they have each:

  1. Ammount
  2. Product_id
  3. User_id

and i can acces the price via carted_product.product in the model asociation

heres my view code:

%tbody

     - if user_signed_in?
       - current_user.carted_products.each do |f|
         - @total = @total + f.carted_product_price(f.ammount, f.product.price)
         %tr
           %td.carted_name
             = f.product.name
           %td.carted_ammount
             = f.ammount
           %td.carted_price  
             $ 
             = f.carted_product_price(f.ammount, f.product.price)

carted_product_price just recieves the ammount and price of the product and returns the multiplied price

so it prints all the rows in the table it doesnt matter if the different records are form the same product, i just want to join them in only one table row output

  • 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-06-08T06:01:51+00:00Added an answer on June 8, 2026 at 6:01 am

    I think you want to iterate through the products, not the carted products. Personally, I’d move most of this logic to the model.

    class Product
      ...
      def carted_products_total_amount
        carted_products.inject(0) {|acc, amount| acc + amount }
      end
    
      def total
        carted_product_price(carted_products_total_amount, price)
      end
    end
    

    And then in the view:

    %tbody
       - current_user.products.each do |product|
         - @total = @total + product.total
         %tr
           %td.carted_name
             = product.name
           %td.carted_amount
             = product.carted_products_total_amount
           %td.carted_price  
             $ 
             = product.total
    

    Also, the correct spelling of “amount” only includes a single “m” character. 😉

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

Sidebar

Related Questions

Ive been trying to sort out output using AWK, and have been pretty successful
I am trying to sort a Vector list using the Java Collections frameworks. This
Possible Duplicate: Sort multidimensional array by multiple keys I am trying to find some
I'm trying to canonicalize the representation of some XML data by sorting each element's
When I am trying to sort a file and save the sorted output in
I am trying to create a child that calls sort. The parent sends data
I'm trying to do a mock-up in Matlab on some image data in a
I am trying to sort an array of a 2 element data structure alphabetically.
I'm trying to incorporate some sort of error handling into an iOS app. After
What I am trying to do is generate some raw output within a module.

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.