In this episode, we start laying the foundation for how the app will keep track of which team a user is currently working with. Imagine having a dropdown in your app listing all the teams you belong to – when you pick one, we want the app to remember and use that choice everywhere. Since each team can have its own projects and resources, tying everything to the current team is super important.
To set things up, we add a new current_team_id
column to the users
table through a migration. This column points to the team that the user is currently active in, and it's nullable so it doesn't break anything at the start. We make sure it's a foreign key referencing the teams
table, to keep things in sync.
Next, we write a test to ensure when a new user is created, their current_team_id
gets set to their "personal team" right away. After some initial failures, we fix the fillable
settings and make sure our code properly sets this field via a relationship on the user model, refining things so it's clean and reusable.
In the end, we confirm with tests and a quick manual database check: new users have their current team set up perfectly from the moment they register. This paves the way for more flexible team switching and resource management as we keep building.