Ruby on Rails - Virtual Attributes -


i have following model:

  create_table "material_costs", :force => true |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 create virtual attribute in model give me cost per square inch of each material?

also have model holds vat value:

  create_table "taxes", :force => true |t|     t.string   "name"     t.decimal  "rate",       :precision => 10, :scale => 0     t.datetime "created_at"     t.datetime "updated_at"   end 

how use model give me total price per square inch each material item ie need add on vat rate?

edit - store vat value in following model:

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

edit - 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 |format|       format.json { render :json => { :moulding => @moulding, :mount => @mount, :glass => @glass, :backing_board => @backing_board, :wastage => @wastage, :markup => @markup } }     end   end 

it doesn't make sense put in table, or need recalculated on every update.

as matchu suggests, should define method in model class.

note: i've added class variable hold tax value.

class materialcost < activerecord::base   # initialize tax rate on initialization of 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 controller code:

class materialscontroller < applicationcontroller    def calculate_full_price     # /materials/calculate_full_price/5      # material in question using id param.     material = materialcost.find(:first, :id => params[:id])      # calculate total price     @total_price_per_sq_in_with_tax = material.total_cost_per_square_inch      # done (fall off render normal view)   end  end 

i'm not quite sure tax use case exactly, make little more sense?


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -