使用 Rails 迁移添加新表..?

piy*_*vra -1 rails-migrations ruby-1.8.7

我想使用 Rails 迁移添加新表:

**table_name** users_location_track
**columns** id (primary key, auto increment serial),
            user_id (reference to users), location_info (string), 
            creation_time(time-stamp)
Run Code Online (Sandbox Code Playgroud)

请建议程序和代码 我是 Rails 新手?

Man*_*ava 8

在 Rails 中,您需要编写如下命令:

 rails generate migration CreateUserLocationTrack user_id:integer location_info:string 
Run Code Online (Sandbox Code Playgroud)

您不需要默认创建的creation_timecreated_at

欲了解更多信息,请关注Rails 指南


piy*_*vra 5

谢谢你的批评。最后我得到了我的答案:

这是为未来想要的人提供的解决方案。

首先进入项目目录然后运行以下命令

rails generate migration add_user_lat_long
Run Code Online (Sandbox Code Playgroud)

然后将生成一个迁移文件,然后您可以按以下样式进行编辑:

class AddUserLatLong < ActiveRecord::Migration
  def self.up
    create_table :users_location_track do |t|
      t.string :location_info
      t.references :user

      t.timestamps

end
    add_index :users_location_track, :user_id, :name =>'index_user_lat_longs_on_user_id'
  end

  def self.down
        drop_table :users_location_track
  end
end
Run Code Online (Sandbox Code Playgroud)