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

  • Home
  • SEARCH
  • 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 3442974
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:44:05+00:00 2026-05-18T08:44:05+00:00

I have the following model: create_table material_costs, :force => true do |t| t.string material

  • 0

I have the following model:

  create_table "material_costs", :force => true do |t|
    t.string   "material"
    t.integer  "height"
    t.integer  "width"
    t.decimal  "cost",       :precision => 4, :scale => 2
    t.datetime "created_at"
    t.datetime "updated_at"
  end

How would I create a virtual attribute in the model to give me the cost per square inch of each material?

Also I have another model which holds the VAT value:

  create_table "taxes", :force => true do |t|
    t.string   "name"
    t.decimal  "rate",       :precision => 10, :scale => 0
    t.datetime "created_at"
    t.datetime "updated_at"
  end

How do I use this model to give me a total price per square inch for each material item ie need to add on the VAT rate?

Edit – I now store the VAT value in the following model:

  create_table "app_options", :force => true do |t|
    t.string   "name"
    t.string   "value"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Edit – This is my controller code:

  def calculate_quote
    @moulding = Moulding.find( params[:id], :select => 'cost, width' )
    @mount = MaterialCost.find(1).total_cost_per_square_mm
    @glass = MaterialCost.find(2).total_cost_per_square_mm
    @backing_board = MaterialCost.find(3).total_cost_per_square_mm
    @wastage = AppOption.find( 2, :select => 'value' )
    @markup = AppOption.find( 3, :select => 'value' )

    respond_to do |format|
      format.json { render :json => { :moulding => @moulding, :mount => @mount, :glass => @glass, :backing_board => @backing_board, :wastage => @wastage, :markup => @markup } }
    end
  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-18T08:44:06+00:00Added an answer on May 18, 2026 at 8:44 am

    It doesn’t really make sense to put that in the table, or it would need to be recalculated on every update.

    As Matchu suggests, you should just define a method in the model class.

    Note: I’ve added a class variable to hold the tax value.

    class MaterialCost < ActiveRecord::Base
      # Initialize the tax rate on initialization of the class
      @@tax = AppOptions.find(:first, :name => 'VAT').value.to_f
    
      # ...
    
      def base_cost_per_square_inch
        cost / (height * width)
      end
    
      def total_cost_per_square_inch
        base_cost_per_square_inch * (1 + @@tax)
      end
    end
    

    And some controller code:

    class MaterialsController < ApplicationController
    
      def calculate_full_price
        # GET /Materials/calculate_full_price/5
    
        # Get the material in question using the ID param.
        material = MaterialCost.find(:first, :id => params[:id])
    
        # Calculate the total price
        @total_price_per_sq_in_with_tax = material.total_cost_per_square_inch
    
        # Done (fall off to render the normal view)
      end
    
    end
    

    I’m not quite sure what your tax use case is exactly, but does that make a little more sense?

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

Sidebar

Related Questions

No related questions found

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.