我一直在閱讀關於Rails中的add_index
方法。我應該在Rails應用程序中索引哪些字段?
在教程中,我發現這個例子:
class AddTitleToMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :title
t.string :content
t.integer :user_id
t.timestamps
end
add_index :microposts, [:user_id, :created_at]
end
end
我不知道爲什麼[:user_id, :created_at]
進行索引,而不是其他人。
我如何知道哪些字段在Rails應用程序中編入索引?
對不起,有簡單的英語**外鍵**? – alexchenco 2012-01-27 06:34:12
外鍵是在表外引用表的鍵。 – 2012-01-27 06:35:41
它們是數據庫用於維護關係的字段。 user_id是一個外鍵,因爲它是將micropost與用戶關聯的字段。如果你願意,這個字段本身就像一個指針,它指向另一個表。 – daniel 2012-01-27 06:37:39