我一直在阅读关于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