0
votes

Ruby on Rails beginner here.

Had this error in localhost:3000

ActiveRecord::PendingMigrationError Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development

I ran rake db:migrate in terminal and got this:

$ rake db:migrate
rake aborted!
SyntaxError: /Users/EuphoriaComplex/src/bookmarks/db/migrate/20150407050503_add_user_to_bookmark.rb:5: syntax error, unexpected tIDENTIFIER, expecting keyword_end

    add has_many :bookmarks to app/models/user.rb
                              ^
/Users/EuphoriaComplex/src/bookmarks/db/migrate/20150407050503_add_user_to_bookmark.rb:7: syntax error, unexpected tIDENTIFIER, expecting keyword_end

    add belongs_to :user to app/model/user.rb
                           ^

And this is my code in bookmarks/db/migrate in Sublime:

class AddUserToBookmark < ActiveRecord::Migration
  def change
    add_column :bookmarks, :user_id, :integer

    add has_many :bookmarks to app/models/user.rb

    add belongs_to :user to app/model/user.rb
  end
end

I was following this tutorial: http://12devs.co.uk/articles/writing-a-web-application-with-ruby-on-rails/ and I only made it to "Require authentication to manage your bookmarks"

"Users have many Bookmarks" is the section in question.

2
A comment at the bottom of the tutorial points out that the add belongs_to :user should point to app/model/bookmark.rbMark Thomas

2 Answers

2
votes

The error tells you exactly where the problem is.

add has_many :bookmarks to app/models/user.rb
add belongs_to :user to app/model/user.rb

this should not be in a migration since they do not change the schema. You need to add these to the bookmark and user model, so

class Bookmark < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :bookmarks
end
0
votes

The relationships between models go in the models, the migrations are for the database, it is different... you should keep the add_column :bookmarks, :user_id, :integer but the other two lines erase them from your migration, you shouldgo to your user.rb model and add has_many :bookmarks and go to your bookmark.rb model and add belongs_to :user

Maybe you can also read this guide, it might help: http://guides.rubyonrails.org/index.html