piyushmandovra
piyushmandovra

Reputation: 4381

adding a new table using rails migration..?

I want to add new table using rails migration:

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

please suggest procedure and code I am new to rails?

Upvotes: -1

Views: 22893

Answers (2)

Manish Shrivastava
Manish Shrivastava

Reputation: 32070

In Rails You need to write a command like below:

 rails generate migration CreateUserLocationTrack user_id:integer location_info:string 

you don't need creation_time as created_at is created by default.

For more information, please follow rails guide.

Upvotes: 8

piyushmandovra
piyushmandovra

Reputation: 4381

thank you for criticizing. Finally I got my answer:

Here's the solution for whoever want in future.

first go to project directory then run following command

rails generate migration add_user_lat_long

and then a migration file will be generate then you can edit in following style:

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

Upvotes: 5

Related Questions