我想创建下表。我通过使用 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
关于ruby-on-rails - 如何格式化此 PostgreSQL 查询以使其能够与 db :migrate 一起运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60922051/