2016-03-01 99 views

回答

2

您可以覆蓋主鍵這樣

class Book < ActiveRecord::Base 
    self.primary_key = 'author' 
end 
+0

我讀到我的書這種做法,它說這就是隻覆蓋列名,例如用於在軌priamry鍵的默認列名是ID,現在它的作者,但數據類型仍然是整數。我的模式中有一個字符串作爲priamry鍵,因此這不是我的問題。 –

+0

[將Rails中的主鍵改爲字符串](http://stackoverflow.com/questions/750413/altering-the-primary-key-in-rails-to-be-a-string#answer-6714889)爲什麼你應該儘量避免這樣做 – MilesStanfield

0

我不知道你想做什麼。這是改變Rails中主鍵的錯誤。

但是對於這個問題,試着在你的遷移中做到這一點。

class Foos < ActiveRecord::Migration 
    def self.up 
     create_table :foos, :id => false do |t| 
      t.string :my_id 
      t.timestamps 
     end 
    end 
end