title: a way to have an model attribute  which is not persist in database as a table column
body: <p>I am using <strong>Rails v2.3.2</strong>.</p>
  
  <p>I have a model:</p>
  
  <pre><code>class Cars &lt; ActiveRecord::Base
  
    tag_nr = rand(2007)
  
  end
  </code></pre>
  
  <p>The <code>Cars</code> model is mapped to the <code>cars</code> table in <strong>database</strong> with columns <code>name</code>, <code>owner</code>.</p>
  
  <p>As you see above, there is also a <code>tag_nr</code> which is basically a <strong>random number</strong>. </p>
  
  <p>I would like to have each <strong>instance object</strong> of <code>Cars</code> <strong>class</strong> hold a <strong>random number</strong> generated like above. <strong>But</strong> I do not want to have this random number be stored in database. And in future, I can access this instance object's <code>tag_nr</code> by:</p>
  
  <pre><code>nr = CAR_INSTANCE.tag_nr
  </code></pre>
  
  <p>And the <code>nr</code> now is the <strong>same</strong> as the <code>tag_nr</code> first generated for this Cars instance object.</p>
  
  <p>So, where and how should I define this random number in my Car model?</p>
  
  <p><strong>-----------What I tried is:-----------</strong></p>
  
  <pre><code>class Cars &lt; ActiveRecord::Base
    after_initialize :init
  
    attr_accessor :tag_nr
  
    def init
      @tag_nr = rand(2007)
    end
  end  
  </code></pre>
  
  <p>But if I retrieve a car from database and try to access <code>tag_nr</code>, I got <code>nil</code>:</p>
  
  <pre><code>@c=Cars.first
  @c.tag_nr.nil? # it returns true
  </code></pre>
  
  <p>So, how can I have an model attribute(like <code>tag_nr</code>) which is not persist in database as a table column but can be accessed as an attribute?</p>