2010-12-01 18 views
0

I以下のモデルがあります:- 仮想属性

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の値を保持している別のモデルを持っていますか?

編集 - 私は今、次のモデルに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 

答えて

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 

私はあなたの税金のユースケースが正確に何であるかはかなりわからないんだけど、それは少しより多くの意味を作るのですか?

+0

'whichTax.rate'を使って正しい料金を選ぶにはどうしたらいいですか? – freshest

+0

@frehest:私の編集を参照してください。それはまったく役に立ちますか? –

+0

すべてをモデルに移動する方法はありますか?そのため、MaterialCostモデルからの総コストを取得すると、VATは既に適用されています。 – freshest

0

それは本当にあたりの仮想属性ではありませんそれは単なる方法ですよね?

def cost_per_square_inch 
    cost/height/width 
end 
+0

このデータを@mount = MaterialCost.find(1、:select => 'cost_per_square_mm') 'コントローラで取得しようとすると、Unknownカラムに 'cost_per_square_mm'というエラーが発生しますか? – freshest

+0

ファインダを使用して仮想属性を検索することはできません。これらは仮想です。つまり、モデルがメモリに存在する限り、モデルにのみ存在しますが、モデルが保存されているもの(データベーステーブルの行など)には存在しません。 – yfeldblum

+3

代わりに '@amount = MaterialCost.find(1).cost_per_square_mm'を使用することができます。 – yfeldblum

関連する問題