我想要创建以下表格。我使用从ActiveRecord运行SQL
alter table items add column sumup numeric generated always as ( quantity * price + (quantity * price* fpa/100) ) stored;
这样做正确吗?
def change
create_table :items do |t|
t.string :itemName
t.string :prom
t.string :promCode
t.string :baseCode
t.string :desc
t.numeric :fpa
t.numeric :price
t.integer :quantity
t.string :monadaMe
t.string :familys
t.timestamps
end
ActiveRecord::Base.connection.execute(" alter table items add column sumup numeric generated always as ( quantity * price + (quantity * price* fpa/100) ) stored;")
end
end
只创建新列:
rails generate migration AddSumupToItems sumup:float
在您的model
中,添加:
before_save :calculate_sumup
def calculate_sumup
sumup = ( quantity * price + (quantity * price* fpa/100) )
self.sumup = sumup
end