Reputation: 1243
Hi I created a table for a wiki application that I was doing. It was pretty simple in the beginning I had a Questions table and an Answers table. I now want to add users to it. So for that I created a users table and the whole signup thing is ready. The wiki part is working and the users part is working but I am having trouble to merge them together.
I first created a Questions as a scaffold as follows:
rails g scaffold Question title:string body:string
Then Answers as follows:
rails g scaffold Answer question_id:integer content:string
I then tried to add the users_id column after creating the Users table:
rails generate migration add_users_id_to_questions user_id:integer
I then tried to index the user_id column by adding the following line in the migration file:
class AddUserIdToQuestions < ActiveRecord::Migration
def change
add_column :questions, :user_id, :integer
end
add_index :questions, :user_id
end
I do rake:db migrate but it doesnt show the change after I run migration. Is there any other way I can index user_id other than by adding a column to the migration file. I even ran rake db:rollback so the addition of user_id column is gone but when I make the change and run rake db:migrate it gives me an error user_id doesnt exist. It would be greatly appreciated if you could help me how to create the index. I running Rails 3, SQLite as my DB. Thank you.
Upvotes: 2
Views: 1276
Reputation: 4807
Rollback and then re-run the migration with add_index
inside the change
block.
class AddUserIdToQuestions < ActiveRecord::Migration
def change
add_column :questions, :user_id, :integer
add_index :questions, :user_id
end
end
Upvotes: 5