2010-05-31 2 views
2

가 나는 attachments 가상 속성과 IncomingEmail 모델이초기화 가상 속성

>> i = IncomingEmail.new 
=> #<IncomingEmail id: nil,...) 
>> i.attachments << "whatever" 

처음 설정하지 않고 i.attachments[] (다른 방법으로 말하자면,이 가상 속성을이 아닌 빈 배열로 지정하기를 원합니다.)

답변

3

사용 after_initialize 콜백

class IncomingEmail < ActiveRecord::Base 
    attr_accessor :attachments 
    def after_initialize 
    self.attachments ||= [] # just in case the :attachments were passed to .new 
    end 
end 
+0

예, 그리고 액티브의 기본 초기화를 오버라이드 (override)하지 않는 – allenwei