DEV Community

loading...
Cover image for Construction report: we create tables for trainers and trainees

Construction report: we create tables for trainers and trainees

kubacky profile image Artur ・2 min read

Third post and 5 followers, please contact with collaboration proposals for influencers on priv.

Today a continuation of an earlier post, we will add a model and database tables for trainer and trainees. The assumption is that these two roles will have separate tables, where after registration a new record will be added with a foreign key referring to the users table.

Here we go!
The console our best friend, so we type:

rails g model trainer

which generates two files containing the model and the migration with the new table. We are going to edit the migration file, which is similarly named: 20210425185512_create_trainers.rb

class CreateTrainers < ActiveRecord::Migration[6.1]
  def change
    create_table :trainers do |t|
      t.references :user, index: true, foreign_key: true
      t.string :description
      t.timestamps
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

A few words about what magic has just happened here. The first field is actually a foreign key to the users table. We are extending this table in a 1:1 relation. Then we have a text field in which the trainer can describe their activity. Timestamp is generated by default by rails CLI. Amazing, isn't it?

We already have trainers, we can slowly think about a marketing campaign. Now it is time for the trainees. The wonders that happen here will stay in your memory for a long time. Of course, the console:

rails g model trainee

Open the migration file (20210425185842_create_trainees.rb) and write:

class CreateTrainees < ActiveRecord::Migration[6.1]
  def change
    create_table :trainees do |t|
      t.references :user, index: true, foreign_key: true
      t.references :trainer, index: false, foreign_key: true
      t.integer :weight
      t.integer :height
      t.date :date_of_birth
      t.timestamps
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Here we are already rubbing up against machine learning, artificial intelligence and interplanetary expeditions. A brief explanation so as not to get lost. The first item, as with the coach, is a reference to the user table. Next we have references, this time to the trainers array, with which we have a many-to-one relation - one trainee has one coach, but one trainer has many trainees. Then we have two fields with weight and height and another one with date of birth.

We have the migrations ready. Let's launch the rocket! In the console:

rake db:migrate

And it's done!
We will expand these tables later, but for now we want to run applications with basic functionality. Let's now move on to editing models. First up for editing is the trainer model, which for now will look more or less like this:

class Trainer < ApplicationRecord
  belongs_to :user

  has_many :trainees
end
Enter fullscreen mode Exit fullscreen mode

In a nutshell, this class is just indicating to our application the relationship between the tables. We have almost the same thing in the trainee model:

class Trainee < ApplicationRecord
  belongs_to :user
  has_one :trainer
end
Enter fullscreen mode Exit fullscreen mode

With little difference in the relation. The trainee has one trainer.

And what's in the next episode? We will create a login and registration form. Stay tuned!

Cover photo: Luna is crazy today and took my flip-flop!

Discussion (2)

pic
Editor guide
Collapse
porbas profile image
Piotr Romańczuk

Hi Artur

I think belongs_to :trainee in Trainer class is wrong and should be removed. Probably copy+paste issue?

Good work otherwise! Keep going!

Collapse
kubacky profile image
Artur Author

Nope, this is definitely not a copy paste error. It's my mistake in creating the application. Thanks!