我有以下类,我想使用money-rails gem monetize它的几个字段。
class LineItem < ActiveRecord::Base
monetize :unit_price_cents
monetize :total_cents
end该模式如下所示:
create_table "line_items", force: :cascade do |t|
t.integer "invoice_id"
t.float "quantity"
t.string "unit_type"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "unit_price_cents", null: false
t.integer "total_cents", null: false
end 由于某种原因,除非我在货币化字段中添加别名,否则我会得到undefined method 'unit_price' for #<LineItem:0x007ffb7881eb80>:
monetize :unit_price_cents, as: :unit_price发布于 2021-01-20 21:54:25
根据Money Rails gem documentation,在这种情况下:as是不必要的。只有当您使用其他数据库列名,或者您希望使用其他名称作为money属性时,才应使用它。
在您的例子中,因为您的db列名已经采用了gem期望的格式。你只需要做
monetize :unit_price_cents然后在LineItem实例上调用unit_price以获取货币化对象
@lineitem = LineItem.first
@lineitem.unit_pricehttps://stackoverflow.com/questions/31442315
复制相似问题