title: Rails: Creating a value object with a validated attribute
body: <p>I have two model that both have identical states (Draft, Live, Inactive).  I liked the idea of DRY'ing my code through a value object.  I created the following code:</p>
  
  <pre><code>class CurrentStatus
    STATUSES = %w"DRAFT LIVE INACTIVE"
    attr_reader :status
  
    def initialize(status)
      stringed_status = status.to_s
      if STATUSES.include?(stringed_status)
        @status = stringed_status
      else
        raise "Invalid state for object Status"
      end
    end
  end
  </code></pre>
  
  <p>And in the model:</p>
  
  <pre><code>class Interest &lt; ActiveRecord::Base
    composed_of :status, :class_name =&gt; 'CurrentStatus', :mapping =&gt; %w(status)
    attr_accessible :description, :name, :status
  </code></pre>
  
  <p>This allows me to successfully perform:</p>
  
  <pre><code>[47] pry(main)&gt; i = Interest.new
  =&gt; #&lt;Interest id: nil, name: nil, description: nil, created_at: nil, updated_at: nil, status: nil&gt;
  [49] pry(main)&gt; i.status = CurrentStatus.new('blech')
  RuntimeError: Invalid state for object Status from /app/models/current_status.rb:10:in `initialize'
  [50] pry(main)&gt; i.status = CurrentStatus.new('DRAFT')
  =&gt; DRAFT
  [51] pry(main)&gt; i
  =&gt; #&lt;Interest id: nil, name: nil, description: nil, created_at: nil, updated_at: nil, status: "DRAFT"&gt;
  </code></pre>
  
  <p>but not:</p>
  
  <pre><code>[48] pry(main)&gt; i.status = 'DRAFT'
  NoMethodError: undefined method `status' for "DRAFT":String
  from ruby-1.9.3-p429/gems/activerecord- 3.2.13/lib/active_record/aggregations.rb:248:in `block (2 levels) in writer_method'
  </code></pre>
  
  <p>So when in InterestsController I call the new method:</p>
  
  <pre><code>  def new
      @interest = Interest.new
  </code></pre>
  
  <p>and pull up the form:</p>
  
  <pre><code>    &lt;div class="field"&gt;
        &lt;%= f.label :status %&gt;&lt;br /&gt;
        &lt;%= f.select(:status, %w"DRAFT REPORT_ONLY LIVE SUSPENDED" ) %&gt;
  </code></pre>
  
  <p>My validation stops me:</p>
  
  <pre><code>  Rendered interests/_form.html.erb (70.1ms)
    Rendered interests/new.html.erb within layouts/application (83.0ms)
  Completed 500 Internal Server Error in 465ms
  
  RuntimeError - Invalid state for object Status:
    app/models/current_status.rb:10:in `initialize'
    activerecord (3.2.13) lib/active_record/aggregations.rb:229:in `block in reader_method'
  </code></pre>
  
  <p>What's a better way for me to write this validation?</p>