title: Gorm (Golang) and database with Single Table Inheritance "type" column
body: <p>I'm experimenting with using Go to read from a database that's been part of an existing Rails app. A few of the models and therefore tables use single table inheritance via a <code>type</code> column. In Rails/ActiveRecord, the presence of this column will create an automatic mapping to the appropriate model. If the table is <code>animals</code> and <code>type</code> is <code>Dog</code>, it will map to the <code>Dog</code> class; if it's <code>Cat</code>, it'll map to the <code>Cat</code> class. I want to setup something similar in Gorm.</p>
  
  <p>Since it doesn't seem like Gorm has a <code>default_scope</code> option for a model, I'm using a new callback.</p>
  
  <pre><code>func scopedSearch(scope *gorm.Scope) {
      tablename := scope.TableName()
      switch tablename {
      case "table_using_sti":
          scope.Search.Where("type = ?", "MyModelName")
      default:
          return
      }
  }
  </code></pre>
  
  <p>And then I register the callback in my <code>main</code> function:</p>
  
  <pre><code>db.Callback().Query().Before("gorm:query").Register("my_plugin:before_query", scopedSearch)
  </code></pre>
  
  <p>When I search against an instance using <code>db.First</code> or the model using <code>db.Model(&amp;MyModel{}).Where(...)</code>, it appears to be working. Is this the right way to handle it? Will this scope be respected by all of the query methods or is there something more direct or thorough?</p>