rails外键

```
lalala12@demo-project:~/workspace/lala (master) $ rails g model song title artist
Running via Spring preloader in process 935
      invoke  active_record
      create    db/migrate/20160302112338_create_songs.rb
      create    app/models/song.rb
      invoke    test_unit
      create      test/models/song_test.rb
      create      test/fixtures/songs.yml
lalala12@demo-project:~/workspace/lala (master) $ rails g model user name
Running via Spring preloader in process 946
      invoke  active_record
      create    db/migrate/20160302112355_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
lalala12@demo-project:~/workspace/lala (master) $ rake db:migrate
== 20160302112338 CreateSongs: migrating ======================================
-- create_table(:songs)
   -> 0.0018s
== 20160302112338 CreateSongs: migrated (0.0019s) =============================

== 20160302112355 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0009s
== 20160302112355 CreateUsers: migrated (0.0010s) =============================

lalala12@demo-project:~/workspace/lala (master) $ rails g migration add_songref_to_user song:references
Running via Spring preloader in process 966
      invoke  active_record
      create    db/migrate/20160302112542_add_songref_to_user.rb
lalala12@demo-project:~/workspace/lala (master) $ rake db:migrate
== 20160302112542 AddSongrefToUser: migrating =================================
-- add_reference(:users, :song, {:index=>true, :foreign_key=>true})
   -> 0.0014s
== 20160302112542 AddSongrefToUser: migrated (0.0015s) ========================

lalala12@demo-project:~/workspace/lala (master) $ rails g migration add_userref_to_song user:references
Running via Spring preloader in process 991
      invoke  active_record
      create    db/migrate/20160302112839_add_userref_to_song.rb
lalala12@demo-project:~/workspace/lala (master) $ rake db:migrate
== 20160302112839 AddUserrefToSong: migrating =================================
-- add_reference(:songs, :user, {:index=>true, :foreign_key=>true})
   -> 0.0013s
== 20160302112839 AddUserrefToSong: migrated (0.0013s) ========================


```

schema.rb是这样的

```rb

ActiveRecord::Schema.define(version: 20160302112839) do

  create_table "songs", force: :cascade do |t|
    t.string   "title"
    t.string   "artist"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
  end

  add_index "songs", ["user_id"], name: "index_songs_on_user_id"

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "song_id"
  end

  add_index "users", ["song_id"], name: "index_users_on_song_id"

end
```


在生成器中还可把字段类型设为 references（还可使用 belongs_to）。例如：
```
$ rails generate migration AddUserRefToProducts user:references
```
生成的迁移如下：

```rb

class AddUserRefToProducts < ActiveRecord::Migration
  def change
    add_reference :products, :user, index: true
  end
end
```


这个迁移会创建 user_id 字段，并建立索引。

```rb

class AddUserReferenceToTester < ActiveRecord::Migration
  def change
    add_column :testers, :user_id, :integer
    add_index :testers, :user_id
  end
end
```