2016-10-19 5 views
1

첫 번째 레일즈 (5) 앱에서 생성시 연결에 대한 기본값을 설정하고 싶습니다. 문서 및 많은 질문을 읽었으므로 아직 답변을 찾을 수 없습니다. 내가 after_initialize :set_default_values를 사용하고 난 당신이 여기에 = 누락 생각 documenttype레일스는 연관을위한 디폴트 값을 설정합니다

def set_default_values 
    unless persisted? 
     self.documenttype || 1 if self.documenttype.nil? 
     self.dateformat ||= 'dd.mm.yyyy' if self.dateformat.nil? 
    end 
end 
+0

'='기호를 잊어 버렸습니다 : 'self.documenttype || = 1' – MrYoshiji

답변

1

의 기본을 설정할

:

self.documenttype || 1 if self.documenttype.nil? 

그것은처럼해야

self.documenttype ||= build_documenttype 

또는 특정 문서 유형 :

self.documenttype ||= Documenttype.find(1) 

편집 : 코멘트 OP에서 이 documenttype 언급은 has_one 협회입니다.

+0

'= '기호를 추가해도 해결되지 않습니다. (# 70289997518120)'Fixtum (# 70289925178420)' –

+0

그래서 documenttype은 다른 유형의 객체 (Documenttype)를 기대하며 기본값 1을 넣으려고하므로이 오류가 발생합니다. 어떤 종류의 변수는'documenttype'입니까? 만약 내가 좀 더 자세한 내용을 줄 수 있다면 내가 도울 수있을거야 – Sajan

+0

그것은 has_one 협회 –

관련 문제