开发者

Ruby on Rails - Virtual Attributes

开发者 https://www.devze.com 2023-01-28 08:57 出处:网络
I have the following model: create_table \"material_costs\", :force => true do |t| t.string\"material\"

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' )
开发者_JAVA技巧
    respond_to do |format|
      format.json { render :json => { :moulding => @moulding, :mount => @mount, :glass => @glass, :backing_board => @backing_board, :wastage => @wastage, :markup => @markup } }
    end
  end


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?


It's not really a virtual attribute per se, it's just a method, right?

def cost_per_square_inch
  cost / height / width
end
0

精彩评论

暂无评论...
验证码 换一张
取 消