title: Model Inheritance in Rails
body: <p>I have a class <code>Item</code> and another <code>Weapon</code>. They share a lot of the same state (e.g. <code>size</code>, <code>value</code>), but <code>Weapon</code> has some extra stuff, like <code>damage</code>, <code>critical</code>, and <code>threat</code>. In raw Ruby, I’d extract these common attributes to a module and compose both of them, or I might subclass <code>Weapon</code> from <code>Item</code> and put the common state in the superclass.</p>
  
  <p>Except I’m using Rails. Single-Table Inheritance doesn’t seem like a nice solution as <code>Weapon</code> has a number of attributes not in <code>Item</code>, and I don’t fancy all the empty DB columns. Polymorphic association doesn’t right either, as it seems to be more useful for sharing associations between models, rather than sharing state. Using a Concern: much the same issue.</p>
  
  <p>Is there anything in Rails that allows me to extend the state of a single model in another, while building an extra table for any additional attributes?</p>
  
  <p>Here's a code example of what I'd like in raw Ruby:</p>
  
  <pre><code>class Item
    attr_accessor :value, :weight, item_type 
  
  class Weapon &lt; Item
    attr_accessor :damage, :threat, :critical, :weapon_type
  
  Weapon.new(value: 12) # =&gt; weapon instance with value 12
  </code></pre>
  
  <p>In Rails, can try adding <code>item_id</code> to <code>Weapon</code> - but then I can't do <code>Weapon.new(value: 12)</code>, I'd have to do <code>Weapon.new(item: Item.new(value: 12))</code>.</p>