Devin
Devin

Reputation: 505

What's the proper way to create a database referencing a user using the devise gem?

I'm creating a simple webapp that tracks calories as a way to learn Ruby on Rails. I'm using the gem devise for users to sign in. My next step is to generate a model. I was going to use:

rails generate model Tracker calories:integer consumed_on:datetime

The problem though is that I don't know how to relate this data to the signed in user. What am I missing from this generate model command?

Upvotes: 0

Views: 68

Answers (1)

Zippie
Zippie

Reputation: 6088

rails generate model Tracker user_id:integer calories:integer consumed_on:datetime

This will generate your model with a foreign key in the database referencing the user table. Of course this is not all you have to do, you have to put other code in your model and controller to combine those two columns, create the views, update the config/routes.rb file ..

Also: Don't forget to run the rake db:migrate to make the relational table in the database.

You can see how its done here (your Tracker model/table is theirs Micropost):

http://ruby.railstutorial.org/chapters/a-demo-app#sec-microposts_resource

Upvotes: 1

Related Questions