class CreateBallots < ActiveRecord::Migration
  def change
    create_table :ballots do |t|
      t.references :user
      t.references :score
      t.references :election
      t.string :key
      t.timestamps
    end
    add_index :ballots, :user
    add_index :ballots, :score
    add_index :ballots, :election
  end
end


结果是:

SQLite3::SQLException: table ballots has no column named user: CREATE  INDEX "index_ballots_on_user" ON "ballots" ("user")/home/muhd/awesomevote/db/migrate/20130624024349_create_ballots.rb:10:in `change'


我以为t.references应该为我处理吗?

最佳答案

您忘记添加“ _id”,如下所示:

add_index :ballots, :user_id


或者,如果您希望它自动索引:

t.references :user, index: true


更多信息:add_indexreferences

高温超导

关于ruby-on-rails - Rails:创建索引时“t.references”不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17478669/

10-13 04:50