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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:10:09+00:00 2026-06-10T06:10:09+00:00

I have a setup in which there are 10 attributes that accept a float

  • 0

I have a setup in which there are 10 attributes that accept a float in a rails form. Each attribute also is associated with a value in my model. If a number is entered on the form for more than one attribute, I need to create a weighted average.

An example would be if I have 10 products, each having a price in my model. In the form, a user can enter in the amount (number of products) for each product. I’d like to calculate a weighted price for those products that have an amount entered.

So how can I create a weighted average that checks which products have amounts entered?

  • 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-10T06:10:11+00:00Added an answer on June 10, 2026 at 6:10 am
    columns_names = ['a','b','c','d'] # array of name of the columns
    
    obj = Model.find(:id) # find the object with id
    
    # loop and get column values that are set
    
    values = columns_names.inject([]) do |arr,column_name|
      arr << obj.column_name if params[column_name].eql?"true" # collect the values if the column set
      arr
    end
    
    #get average
    if values.blank?
      # no column selected
    else
      avg =  values.reduce(:+)/values.size
    end
    

    check this for help on weighted average

    http://www.dzone.com/snippets/weighted-mean

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

Sidebar

Related Questions

Here's the setup: I have several tables that hold information for data objects which
I have a setup with a traditional form that's checked with jQuery Tools validator
I have a view setup in Interface Builder which contains a viewController that loads
I have a layout setup which can be view here: http://jsfiddle.net/Pn3ts/ It all works
I have setup my site with 5 pages which are my menu options. When
I have setup BPS and have created a few BPEL processes which i can
I have a Visual Studio Setup project which has a Application Folder DefaultLocation set
I have a Setup project (MSI) in VS2008 which installs my project. Within the
I have just setup a new local site (localhost/pfb.net/) which has been cloned from
We have just setup our hudson server to build .NET projects which seems to

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.