如何在Rails模型中更新BigDecimal属性?

时间:2016-03-07 07:40:11

标签: ruby-on-rails activerecord bigdecimal

我有以下模型JobRequest :: Receipt。

你可以看到:amount属性是一个大十字形。

list1 = [1, 2, 3, 4, 5]
excluded_index = [1]
new_list = [i for n, i in enumerate(list1) if n not in excluded_index]
>>> new_list
[1, 3, 4, 5]

excluded_index = [1, 3]
>>> [i for n, i in enumerate(list1) if n not in excluded_index]
 [1, 3, 5]

这就是对象的样子

# Table name: job_request_line_headers
#
#  id                     :integer          not null, primary key
#  type                   :string(255)
#  job_request_id         :integer
#  merchant_company_id    :integer
#  merchant_id            :integer
#  default_setting        :boolean          default(FALSE)
#  payment_amount         :decimal(12, 4)
#  payment_state          :integer          default(0)
#  payment_at             :datetime
#  deleted_at             :datetime
#  published_at           :datetime
#  delivered_at           :datetime
#  created_at             :datetime
#  updated_at             :datetime
#  related_line_header_id :integer
#  amount                 :decimal(12, 4)

如果我尝试以下面的任何方式更新amount属性,它将提交SQL,但它不会更新值

撬(主)GT; JobRequest :: Receipt.last.update_attribute(:amount,22.3)

撬(主)GT; JobRequest :: Receipt.last.update_attribute(:amount,BigDecimal.new(22.3,5)

金额= BigDecimal.new(22.3,5)

撬(主)GT; JobRequest :: Receipt.last.update_attribute(:金额,金额)

其中任何一项都将返回金额设为零。

> JobRequest::Receipt id: 24, type: "JobRequest::Receipt",
> job_request_id: 19, merchant_company_id: nil, merchant_id: nil,
> default_setting: false, payment_amount: nil, payment_state: 1,
> payment_at: nil, deleted_at: nil, published_at: nil, delivered_at:
> nil, created_at: "2016-03-07 06:58:49", updated_at: "2016-03-07
> 06:58:49", related_line_header_id: nil, amount:
> #<BigDecimal:7ffe424c7d48,'0.0',9(18)

任何线索我做错了什么?

1 个答案:

答案 0 :(得分:0)

我只是在“herouku run rails console”上以下面的方式在Bigdecimal字段上做到了:

$ i = Item.find(47)
$ i.selling_price = 25.00
$ i.save!
$ i.selling_price

OUTPUT: BigDecimal:7f3cdfe1d598,'0.25E2',9(18)