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

The Archive Base Latest Questions

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

Noob question for Ruby on Rails- here’s an example of my situation: If I

  • 0

Noob question for Ruby on Rails- here’s an example of my situation: If I have model circle and attribute radius, where do I do the calculations for circumference? Would this be in the model or the controller, and how might it look? circumference would need to be accessible in my views.

Also, would I be correct in thinking that I don’t need to make circumference an attribute that’s part of my model/database since it can be derived from a user-input radius?

  • 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-31T16:45:29+00:00Added an answer on May 31, 2026 at 4:45 pm

    The logic for calculating a derived attribute absolutely belongs in the model. The circumference is a property of the circle itself, not a concern of how you’re presenting it in the web interface.

    In order to access the circumference from anywhere, just define a method on the class, such as the following:

    require 'mathn'
    class Circle < ActiveRecord::Base
      # assume `radius` column exists in the database
    
      def circumference
        Math::PI * 2 * radius
      end
    end
    

    Since it’s pretty cheap computationally to calculate the circumference, you can just calculate it as needed. If it were something that involved more complex calculation that you didn’t want to run multiple times, you could memoize it as follows:

    def circumference
      @circumference ||= Math::PI * 2 * radius
    end
    

    That would set the @circumference instance variable the first time the method is called, then return the result of the first calculation on every subsequent call. If you were doing that, you’d need to make sure to set @circumference to nil when the radius changed to make sure it’s accurate.

    def radius=(value)
      @circumference = nil
      super(value)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a noob question here. I have an app that does number calculations. I support
Noob question here! I have an array with hashes that looks like this: arr
Noob question. I have this situation where I have these objects: class Address {
PowerShell noob question here. I have groups of servers that are listed in separate
I have a noob question here. I'm getting my head around the C++ structure
I am a noob in ruby on rails so excuse this question if it
noob question here. I have a has_many :through relationship between items and tags. Finding
a noob question here. I have the following code: - (IBAction)selectExistingPicture { if ([UIImagePickerController
Noob Question here. I have created and successfully added an additional Page Layout to
A bit of a noob question here... I have a javascript function on 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.