I am trying to implement two new user types: teachers and students
As such, I want to make two different tables for both user types which will reference the main table like so:
teachers table
id
user_id
first_name
last_name
1
1 (reference to “users”)
Bob
White
students table
id
user_id
first_name
last_name
1
2 (reference to “users”)
Joe
Brown
I generated a new migration and created the “teachers” table like so:
create table("teachers") do
add :user_id, references("users")
add :first_name, :string
add :last_name, :string
end
I then defined a schema for the teachers table like this:
schema "teachers" do
field :first_name, :string
field :last_name, :string
belongs_to :user, User
Lastly, I added this to the user schema:
has_many :teachers, Teacher
I have now successfully added the teachers table with my desired columns. The problem is, my registration form is only accommodating one changeset. The user schema does all the validation and adds the user into the “users” table when the registration is successful. But the “teachers” table remains empty. What would be the best practice to populate the teachers table with the registering user’s info?