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

标签 ruby-on-rails-3 postgresql heroku

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

我有一个名为“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/

相关文章:

python - sqlalchemy.exc.NoReferencedTableError : Foreign key associated with column X could not find table Y with which to generate a foreign key 错误

postgresql - 从 Postgres 的 jsonb 中选择匹配元素

postgresql - 错误: missing FROM-clause entry for table "cte" while executing query in CTE

Python heroku -app 与 buildpack 不兼容

javascript - Heroku Node 部署 "internal server error"

ruby-on-rails - 如何使用 Rails send_data 从 AJAX 帖子触发下载

ruby-on-rails - Nokogiri XML 导入提要组织?

mysql - Xeround 与 ClearDB 托管的 MySQL 优缺点?

ruby-on-rails - 如何在嵌套 Rails 表单中引用模型的现有实例?

ruby-on-rails - Rails - 如何将 HTML 转换为文本