Connecting polls to users
As mentioned previously, we did a lot of work when setting up user accounts in our system but nothing to actually connect, or even restrict access to, polls and user accounts. Thinking back to how we've set up other associations, there are a couple of steps that we have to implement before all of our polls are correctly linked to users. They are as follows:
- We need to link our tables by creating a migration that adds
user_id
to ourpolls
table. - We then need to modify the code in our schemas to represent the addition of the association.
- We need to modify all of the creation code in the context to set the poll's
user_id
to the appropriate user. - Finally, we need to modify all of the
delete
andupdate
code to only allow those functions to happen if the current user is the owner of that poll.
Creating the migration
We'll begin by creating the migration, as that's the foundation that will allow us to build and complete the rest of the code. Let's create a new migration as follows...