Initialize virtual attributes

Posted by Horace Loeb on Stack Overflow See other posts from Stack Overflow or by Horace Loeb
Published on 2010-05-31T18:54:26Z Indexed on 2010/05/31 19:03 UTC
Read the original article Hit count: 284

I have an IncomingEmail model with an attachments virtual attribute:

class IncomingEmail < ActiveRecord::Base  
  attr_accessor :attachments
end

I want the attachments virtual attribute to be initialized to [] rather than nil so that I can do:

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

Without first setting i.attachments to [] (put another way, I want this virtual attribute to default to an empty array rather than nil)

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about attributes