mysql - How do i add a summation column to my table on Rails 4 -


i have existing table "registers" columns; :full_name, :amount_paid, :user_id, :course_id. have course table columns: :title, :price.

i want add column registers table calculate balance register.course.price , register.

so tried

rails g migration add_balance_to_registers balance:"register.course.price - register.amount_paid"

but when try

rake db:migrate 

i error

you can add field registers table, populate appropriate values.

first add field new migration

rails g migration addbalancetoregisters balance:float

then start migraation add field registers table rake db:migrate

but field balance empty, have populate it.

you create rake task this.

inside lib/tasks/ folder add new file registers.rake.

inside registers.rake add folowing code:

namespace :registers   desc "populate registers balance appropriate balance amount"   task :sync_balance => :environment     register.all.each |r|       b = r.course.price - r.amount_paid       r.update balance: b     end   end end 

then run rake task,

rake registers:sync_balance


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -