Forum: Ruby on Rails Add member variable to ActiveRecord model

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Martin Smith (Guest)
on 2006-05-04 23:25

I have an addition member variable (no DB column) that I would like on
my model called postcode.

To make it available to the view when creating a new record I have added
the following:

  def initialize
    @postcode = ""

But now all the other members on my model (the ones that should be there
after are gone, and the view reports a nil error...

  You have a nil object when you didn't expect it!
  You might have expected an instance of Array.
  The error occured while evaluating nil.include?

  Extracted source (around line #110):

  107:       <label for="entity_name">Name</label>
  108:       </span>
  109:       <span class="forminput">
  110: <%= text_field 'entity', 'name'  %>
  111:       </span>
  112: </div>

If I remove the initialize method they reapear, but then, of course,
postcode is nill and the rendering fails with the same error.

This topic is locked and can not be replied to.