Ruby on rails 运行GENERATE命令时缺少Rails add_索引
因为我是Rails新手,所以我在Rails网站上关注这一点 在第6.1节生成模型中,我应该运行Ruby on rails 运行GENERATE命令时缺少Rails add_索引,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,因为我是Rails新手,所以我在Rails网站上关注这一点 在第6.1节生成模型中,我应该运行rails生成模型注释注释注释器:string body:text post:references,以获取注释模型 据推测,迁移文件中应该包括以下内容: class CreateComments < ActiveRecord::Migration def change create_table :comments do |t| t.string :commenter
rails生成模型注释注释注释器:string body:text post:references
,以获取注释模型
据推测,迁移文件中应该包括以下内容:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
class CreateComments
但在我的迁移文件中,除了这一行之外,我还有所有内容add\u index:comments,:post\u id
。
相反,我在t后面有index:true
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
我似乎找不到解释,有人能解释一下这里发生了什么吗?因为稍后我需要使用:post_id,但在我的迁移版本中,它没有明确声明。我很困惑。在上面的代码中,如果您想为post\u id创建索引,那么将其写为t.integer:post\u id
否则,您可以尝试此添加索引:评论,:post
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
这将产生以下代码:
class AddPartNumberToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_index :products, :part_number
end
end
class AddPartNumberToProducts
发布准确的迁移文件很好,但这是使用“index:true”的新功能吗?
class AddPartNumberToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_index :products, :part_number
end
end