Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 运行GENERATE命令时缺少Rails add_索引_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 运行GENERATE命令时缺少Rails add_索引

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新手,所以我在Rails网站上关注这一点

在第6.1节生成模型中,我应该运行
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