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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:21:26+00:00 2026-05-24T17:21:26+00:00

Suppose I have the following structure: parts model: – title – weight – part_type_id

  • 0

Suppose I have the following structure:

parts model:
- title
- weight
- part_type_id

part_type model:
- quotation

I want to set a “price” for each part. The models are using associations (parts:has_one) and (part_types:belongs_to). This is ok.

So, basically what I’m doing is creating an virtual attribute like this:

class Parts < ActiveRecords::Base
  ..
  attr_accessor :price

  def price
    quotation = PartType.find(self.part_type_id).quotation
    price = self.weight * quotation
  end
end

And I can call it from the view like this:

<% @parts.each do |part| %>
   <%= part.title %>
   <%= part.price %>
<% end %>

Is this the “right” way for doing this or how should I do?

  • 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-24T17:21:27+00:00Added an answer on May 24, 2026 at 5:21 pm

    You might want to use attr_reader to be paranoid:

    attr_reader :price
    

    Changing the price on its own doesn’t make much sense so, since they really are out to get you, it is best to do away with a whole class of possible bugs with that small change. Then you’d want to alter your accessor to directly work with the instance variable that attr_reader creates:

    def price
        return @price if(@price)
        @price = PartType.find(self.part_type_id).quotation * self.weight
    end
    

    Presumably neither nil nor false are valid prices so you don’t have to worry about computing the @price over and over again if if(@price) fails; the computation should only occur once as instance variables are automatically initialized to nil on first access.

    Looks sensible enough to me other than the minor nit mentioned above.

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

Sidebar

Related Questions

suppose you have the following structure: #include <windows.h> // BOOL is here. #include <stdio.h>
Suppose I have the following (desired) folder structure: *CommonProject *Project#1 ----> CommonProject(link) *Project#2 ---->
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
Suppose I have the following class structure: [XmlInclude(typeof(CustomNode))] [XmlInclude(typeof(CustomNode2))] [XmlRoot(node)] class Node { [XmlElement(node)]
Suppose I have the following directory structure: workspace/ __init__.py ys_manage/ __init__.py manage.py ys_utils/ __init__.py
Suppose I have a directory structure like the following /a/b/testB.xml /a/c/testC.xml /a/testD.xml and I
Let's suppose I have the following structure in my project (I'm using iBatis as
Suppose you have a data frame with the following structure: df <- data.frame(a=c(1,2,3,4), b=c(job1;job2,
Suppose I have the following structure: app/ __init__.py foo/ a.py b.py c.py __init__.py a.py,

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.