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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:43:59+00:00 2026-05-27T16:43:59+00:00

I want to loop a calculation that is based on users params. Example in

  • 0

I want to loop a calculation that is based on users params.

Example in my index action I have:

@com = Company.where(:online => true).order('position')

And I my table I have:

Name (varchar)
Price1 (integer)
Price2 (integer)
Price1in (integer)
Price2in (integer)

In view I then have my simple loop:

<%= com.name %><br />
<%= com.price1 %><br />
<%= com.price2 %>

I have created a instance method to do some calculation.
Model:

  def sum_price(x)
    (((x - self.price1in) * (60 * self.price1)) / 100))
  end

   def sum_price2(x)
    (self.price2 * x / 100)
  end

I can now do:

<% @com.each do |com| %>

    <%= com.name %><br />
    <%= com.price1 %><br />
    <%= com.price2 %><br />
    <%= com.sum_price(params[:price]) %><br />
    <%= com.sum_price2(params[:price2]) %>
<% end %>

But I want to order by total_price which is com.sum_price2(params[:price]) + com.sum_price2(params[:price2]) and I want to be able to call it in view as:

<% @com.each do |com| %>
    <%= com.name %><br />
    <%= com.total_price %><br />
<% end %>
  • 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-27T16:43:59+00:00Added an answer on May 27, 2026 at 4:43 pm

    This should work:

    class Company
      include Comparable
      attr_accessor :total_price
    
      def <=> other
        self.total_price <=> other.total_price
      end
    
      def sum_price(x)
        (((x - self.price1in) * (60 * self.price1)) / 100))
      end
    
      def sum_price2(x)
        (self.price2 * x / 100)
      end
    
      def calculate_total price1, price2
        @total_price ||= sum_price(price1) + sum_price2(price2)
      end
    end
    
    #controller
    @companies = Company.all
    @companies.each{|company| company.calculate_total(params[:price], params[:price2])}
    @companies.sort!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a directory of pictures that I want to loop through and do
I want to loop over the files that begin with '.' in directory x
I have a listbox and I want to loop through each of the items
I want to display the progress of a calculation done with a DO-loop, on
I have an ai loop that I would like to write for my iphone
I have a loop of intensive calculations, I want them to be accelerated using
I want to run a for loop which makes a calculation only for specific
what i have here works exactly how i want it. My question is that
I have some data calculation method (let it be myMethod:), and I want to
I want to loop over the contents of a text file and do 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.