Joe Saunders
Joe Saunders

Reputation: 798

How do I setup Rails Associations with Devise and scaffold generated models?

I'm using rails 3.1 with Devise for user authentication...

I'm having trouble either getting the associations between two scaffold generated models working or I'm not accessing it correctly.

I have two models: User (Devise), and Post

I've added the belongs_to :user tag in the Post model and has_many :posts in the User model.

When I open a rails console, rails c from terminal in the app directory, I thought I should be able to reference the Post model through the user model. Such as User.post.count but I keep getting a NoMethodError: undefined method "post" error.

I've been through all the guide.rails.org and back through the railstutorial.org book and I'm definitely missing something somewhere.

Any help would be much appreciated.

Upvotes: 2

Views: 636

Answers (1)

TheDelChop
TheDelChop

Reputation: 7998

Have you added your migrations that adds user_id to the posts table? Without that foreign key, Rails can't figure out which users are associated with which posts.

Upvotes: 2

Related Questions