简体   繁体   English

Ruby on Rails-虚拟属性

[英]Ruby on Rails - Virtual Attributes

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

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. 正如Matchu所建议的,您应该只在模型类中定义一个方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM