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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:55:57+00:00 2026-06-17T20:55:57+00:00

I’m a bit of a noob programmer so apologies if the question isn’t clear

  • 0

I’m a bit of a noob programmer so apologies if the question isn’t clear enough.

I’m trying to create a basic rails app where I have 3 different tables: usages(month, usage), prices(month, price) and spends(month, spend).
I’m trying to get it so that spend = usages.usage * prices.price. I’ve put the following code into my Spend model:

class Spend < ActiveRecord::Base
  c = Usage.all.count
      i = 1
      while i <= c
          u = Usage.find(i)
          p = Price.find(i)
          Spend.create(month:u.month, spend:u.usage*p.price)
          i += 1
      end
end

This works great initially, but as soon as I start adding and removing usages and prices, their id’s change so it isn’t as clear cut. How can I do this in a much better way?

Thanks,

Kev

  • 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-17T20:55:59+00:00Added an answer on June 17, 2026 at 8:55 pm

    In this case, I would lean against making a separate Spend model, since all it does is calculate data that is already present in the database. Unless you have severe caching requirements (and I doubt it in your case), you can use simple instance methods to retrieve the data you want.

    First figure out how your Usage and Price models are related. Since you seem to be associating them by id, it appears to be a one-to-one relationship (correct me if I’m wrong on this). However, associating by assuming they have the same primary key is a dangerous approach – rather have one model point to the other using a foreign key. We’ll pick the Price model to hold a primary key for Usage, but the reverse can also work. You’ll need to add a column using a migration like this:

    def change
      add_column :prices, :usage_id, :integer
    end
    

    Your models should then look like this:

    class Usage < ActiveRecord::Base
      has_one :price
    
      def spend
        usage * price.price
      end
    end
    
    class Price < ActiveRecord::Base
      belongs_to :usage
    end
    

    And you can find your spend value for an individual usage item like this:

    usage = Usage.find(some_id)
    puts usage.spend
    

    Or you can get multiple ‘spends’ like this:

    Usage.include(:price).each do |usage|
      puts usage.spend
    end
    

    I’ve left out any reference to month, as I’m not sure how you are using it or if it’s needed at all for calculating spend.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.