【发布时间】:2014-11-10 00:58:41
【问题描述】:
我正在关注《使用 Rails 4 进行敏捷 Web 开发》一书,但似乎在理解规模应该做什么或我的代码不正确方面存在问题。我将价格字段的精度设置为 8,比例设置为 2。我希望这意味着当我以某种形式添加产品时,无论我输入什么数字,它都只会显示小数点后 2 位数字。例如,输入价格为 45.12345 会在产品创建后显示为 45.12,但我看到的整体与我输入的完全一样。
这里是 schema.rb:
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141109160004) do
create_table "products", force: true do |t|
t.string "title"
t.text "descritpion"
t.string "image_url"
t.decimal "price", precision: 8, scale: 2
t.datetime "created_at"
t.datetime "updated_at"
end
end
【问题讨论】:
-
您使用的是什么数据库。默认的 sqlite3?
-
是的 sqlite3,我原以为这本书会提到这个限制,因为它建议您使用该数据库。