2013-05-27 2 views
0

레일 3.1을 사용하고 있습니다.레일 : attr_accessor의 validates_numericality_of가 예상대로 작동하지 않습니다.

class Product < ActiveRecord::Base 
    attr_accessor :output_unit 
    ... 
    validates_numericality_of :output_unit, greater_than_or_equal_to => 0, :on => update 
    ... 
end 

나는 제품을 업데이트하려고 내가이 분야에 다수 소개, 양식 메시지 표시 "출력 단위는 숫자가 아닌":이 같은 모델 제품 무언가가있다.

고맙습니다. 당신은 방법 output_unit_before_type_cast를 작성하고 self.output_unit를 반환 할 수 있습니다

+0

데이터베이스에서 "output_unit"의 데이터 유형은 무엇입니까? (예 :'db/schema.rb'에 표시) – lurker

+0

이 속성은 데이터베이스에 저장되지 않고 가상 속성입니다. –

답변

0

,이 시도 :

attr_accessor :output_unit 

validates_numericality_of :output_unit, :only_integer => true, greater_than_or_equal_to => 0, :on => update 

def output_unit_before_type_cast 
    self.output_unit 
end 

또는

attr_accessor :output_unit 
validates :output_unit, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0, :on => update } 

def output_unit_before_type_cast 
    self.output_unit 
end 
+0

난 그냥 시도하지만 그 결과는 동일한 숫자 "는 숫자가 아닙니다"소개 때 동일합니다. –

+0

모든 답변이 작동하지 않으면'self.'를 제거하십시오. –

+0

자기를 제거하려고했지만 같은 결과가 나타납니다. ( –

관련 문제