gpt4 book ai didi

ruby-on-rails-3 - 在 Heroku 上的 Postgres 中存储 float 组

转载 作者:行者123 更新时间:2023-11-29 13:04:14 25 4
gpt4 key购买 nike

我该怎么做呢?是否可取?

我有一个名为“Item”的表,其中包含“name”和“price”列。

价格一直在变化,我想存储所有变化以便绘制图表。

我在想“价格”应该是一个数组?这是正确的吗?

谢谢

最佳答案

我会使用单独的表格来跟踪价格历史记录。像这样简单的东西:

create_table :item_prices do |t|
t.integer :item_id, :null => false
t.decimal :price, :null => false, :precision => 7, :scale => 2
t.timestamps
end

请注意,价格小数,而不是 float 。永远不要使用 float 来换取金钱。

然后在 Item 中,是这样的:

class Item
has_many :item_prices
before_save :update_price_history, :if => :price_changed?
private
def update_price_history
self.item_prices.create!(:price => self.price)
end
end

这样做的一个好处是,您可以跟踪价格何时变化以及价格本身,这可能会使您的图表看起来更合理一些。

关于ruby-on-rails-3 - 在 Heroku 上的 Postgres 中存储 float 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19435922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com