Ruby on Rails - 虚拟属性

时间:2010-12-01 16:05:59

标签: ruby-on-rails

我有以下型号:

  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

如何在模型中创建虚拟属性以获得每种材料的每平方英寸成本?

另外,我还有另一个持有VAT值的模型:

  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

如何使用此模型为每个材料项目提供每平方英寸的总价格,即需要增加增值税率?

修改 - 我现在将VAT值存储在以下模型中:

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

修改 - 这是我的控制器代码:

  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

2 个答案:

答案 0 :(得分:3)

将它放在表中是没有意义的,或者需要在每次更新时重新计算。

正如Matchu建议的那样,你应该在模型类中定义一个方法。

注意:我添加了一个类变量来保存税额。

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

一些控制器代码:

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

我不太确定你的税务用例究竟是什么,但是这样做会更有意义吗?

答案 1 :(得分:0)

它本身并不是一个虚拟属性,它只是一种方法,对吗?

def cost_per_square_inch
  cost / height / width
end