2012-03-31 4 views
0

, 나는이 오류 시작하기 :0에 대해 정의되지 않은 메소드 'to_d': 데이터베이스를 시드하려고 할 때 Fixnum? 내 데이터베이스를 종자려고 할 때

:

rake aborted! 
undefined method `to_d' for 0:Fixnum 

나는 쉼표의 내 소수 열 스트립이 방법을 추가 한 후이 시작을 그들의 현재 (2,000.00 < 2000.00) 경우

create_table :products do |t| 
    t.decimal :cost, :precision => 11, :scale => 2 
    t.decimal :tax_rate, :precision => 8, :scale => 2 
    t.decimal :shipping_cost, :precision => 8, :scale => 2 
    t.decimal :cost_per_unit, :precision => 11, :scale => 2 
    t.decimal :unit_amount, :precision => 16 

:

def cost=(num) 
    num.gsub!(',','') if num.is_a?(String) 
    self[:cost] = num.to_d 
end 

나는 나의 Product 모델의 열 각각에 대해이 같은 방법을

이 Product.rb

before_validation :default_to_zero_if_necessary, :on => :create 

private 

    def default_to_zero_if_necessary 
    self.tax_rate = 0 if self.tax_rate.blank? 
    self.shipping_cost = 0 if self.shipping_cost.blank? 
    self.cost = 0 if self.cost.blank? 
    self.cost_per_unit = 0 if self.cost_per_unit.blank? 
    self.unit_amount = 0 if self.unit_amount.blank? 
    end 

어쩌면 내가 더 이상 이런 식으로 내 default_to_zero_if_necessary 방법을 사용할 수 없습니다 : 이제 (https://gist.github.com/2265580에서) 추적을보고 나는 문제가 내 default_to_zero_if_necessary 방법이 될 수있다 생각하십니까? 여기서 정말로 무슨 일이 일어나고있는거야? 나는 완전히 확신하지 못한다.

답변

2

비용을 0 대신 0.0으로 설정 하시겠습니까?

self.cost = 0.0 if self.cost.blank? 

이렇게하면 문제가 해결됩니다.

+0

와우, 그저 간단한 것. 그건 속임수 였어. 고마워. – LearningRoR

관련 문제