首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义的方法`to_d‘on Heroku

未定义的方法`to_d‘on Heroku
EN

Stack Overflow用户
提问于 2012-11-12 18:01:45
回答 1查看 470关注 0票数 1

我有一个部署到Heroku的应用程序,当我试图在数据库中创建一个新的Choice时,它会给我以下错误。我使用virtual attributes在应用程序中处理整数形式的钱。它在我自己的机器上工作得很好,但是当部署到Heroku时,to to_d方法失败。原因:)

代码语言:javascript
复制
2012-11-12 09:48:54+00:00 app web.1 - -
2012-11-12 09:48:54+00:00 heroku router - - POST www.mydomain.com/choices dyno=web.1 queue=0 wait=0ms service=68ms status=500 bytes=643
2012-11-12 09:48:54+00:00 app web.1 - -
2012-11-12 09:48:54+00:00 app web.1 - - Started POST "/choices" for 87.60.190.9/x1-6-00-16-cb-c1-a8-b7.k989.webspeed.com at 2012-11-12 09:48:54 +0000
2012-11-12 09:48:54+00:00 app web.1 - -
2012-11-12 09:48:54+00:00 app web.1 - - NoMethodError (undefined method `to_d' for 6600:Fixnum):
2012-11-12 09:48:54+00:00 app web.1 - - app/models/choice.rb:27:in `value_in_currency'
2012-11-12 09:48:54+00:00 app web.1 - - app/controllers/choices_controller.rb:9:in `create'
2012-11-12 09:48:54+00:00 app web.1 - -
2012-11-12 09:48:54+00:00 app web.1 - -
2012-11-12 09:48:54+00:00 app web.1 - - Processing by ChoicesController#create as HTML
2012-11-12 09:48:54+00:00 app web.1 - - Parameters: {"utf8"=>"✓", "authenticity_token"=>"YygjksxB+1SXnJ8j4jvTtxnh2JfJELpCeHysGrvItcU=", "choice"=>{"spotarea_id"=>"1", "product_id"=>"1", "value_in_currency"=>"66", "exposure"=>"80", "deliverydate"=>"2012-11-13"}, "commit"=>"Create Choice"}
2012-11-12 09:48:54+00:00 app web.1 - - Completed 500 Internal Server Error in 38ms

我的Choice模型包含以下内容:

代码语言:javascript
复制
class Choice < ActiveRecord::Base
  belongs_to :user
  belongs_to :spotarea
  belongs_to :product
  has_many   :bets,       :dependent => :destroy

  attr_accessible     :deliverydate, :exposure, :value_in_currency, :user_id, :spotarea_id, :product_id
  attr_accessible     :deliverydate, :exposure, :value_in_currency, :user_id, :spotarea_id, :product_id, as: :admin

  # Validations
  validates   :spotarea, :product, :value_in_currency, :exposure, :presence => { :message => "is missing!" }
  validates   :value_in_currency, :exposure, :numericality => { :message => "is not numeric!" }
  validates   :value_in_currency, :format => { :with => /^\d+\.?\d{0,2}$/, :message => "is not correctly formatted!" }

  def value_in_currency
    value.to_d/100 if value?
  end

  def value_in_currency=(currency)
    self.value = currency.to_d*100 if currency.present?
  end
end

编辑:

我想我找到了一个解决方案。我重写了代码,因此它现在包含以下内容。我还是不明白为什么to_d不能工作。

代码语言:javascript
复制
def value_in_currency
  BigDecimal(value.to_s)/100 if value?
end

def value_in_currency=(currency)
  self.value = BigDecimal(currency.to_s)*100 if currency.present?
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-21 04:22:17

我想我找到了一个解决方案。我重写了代码,因此它现在包含以下内容。我还是不明白为什么to_d不能工作。

代码语言:javascript
复制
def value_in_currency
  BigDecimal(value.to_s)/100 if value?  
end

def value_in_currency=(currency)
  self.value = BigDecimal(currency.to_s)*100 if currency.present?
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13341635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档