title: Rails: How do I create a default value for attributes in Rails activerecord's model?
body: <p>I want to create a default value for an attribute by defining it in ActiveRecord. By default everytime the record is created, I want to have a default value for attribute <code>:status</code>. I tried to do this:</p>
  
  <pre><code>class Task &lt; ActiveRecord::Base
    def status=(status)
      status = 'P'
      write_attribute(:status, status)
    end
  end
  </code></pre>
  
  <p>But upon creation I still retrieve this error from the database:</p>
  
  <pre><code>ActiveRecord::StatementInvalid: Mysql::Error: Column 'status' cannot be null
  </code></pre>
  
  <p>Therefore I presume the value was not applied to the attribute. </p>
  
  <p>What would be the elegant way to do this in Rails?</p>
  
  <p>Many thanks.</p>