Ruby Rails - how to update database with a calculation? -


i'm working on simple rails app. user enters 2 separate integers , when press 'create', 'show' page displays 2 values , third value calculated user input. have put arithmetic calculate third value in 'show' html.erb view, can't work out how add third value database. heres code 'show' view:`

name: <%= @startup.name %>

<p> <strong>revenue:</strong> <%= @startup.revenue %> </p>  <p> <strong>costs:</strong> <%= @startup.costs %> </p> <h2> <strong>profit:</strong> <%= @startup.profit = @startup.revenue - @startup.costs %> 

you add column database calculated value, , update upon completing calculation.

create migration add column profit startup model:

rails g migration addprofittostartups profit:decimal 

with note may choose different datatype profit depending on datatype revenue , costs columns are. there other helpers currency, see here.

this generate:

class addprofittostartups < activerecord::migration   def change     add_column :startups, :profit, :decimal   end end 

now run rake db:migrate update database.

after this, code have in view should work is.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -