2010-12-01 3 views
0

나는 다음과 같은 모델이 있습니다- 가상 속성

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 

이 어떻게 나에게 각 재료의 평방 인치 당 비용을 제공하는 모델에서 가상 속성을 만들 것입니까? 또한

내가 한 부가가치세 값 보유하고 다른 모델 :

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 값 저장 : - :

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

@freshest : 내 편집을 참조하십시오. 그게 전혀 도움이 되나요? –

+0

모든 것을 모델로 옮기는 방법이 있습니까? 그래서 MaterialCost 모델로부터 총 비용을 얻었을 때 부가가치세가 이미 적용되었습니다. – freshest

0

정말 당 가상 속성이 아니다

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

편집를이 내 컨트롤러 코드 그게 방법 일 뿐이지?

def cost_per_square_inch 
    cost/height/width 
end 
+0

'@mount = MaterialCost.find (1, : select =>'cost_per_square_mm ')'컨트롤러에서 알 수없는 열'cost_per_square_mm '오류가 발생합니다. – freshest

+0

찾기를 사용하여 가상 속성을 찾을 수 없습니다. 그것들은 가상입니다. 즉 모델이 메모리에 존재하는 한 모델에만 존재하지만 모델이 저장되는 곳 (예 : 데이터베이스 테이블의 행)에는 존재하지 않습니다. – yfeldblum

+3

대신에 @amount = MaterialCost.find (1) .cost_per_square_mm'을 사용할 수 있습니다. – yfeldblum