A Beginner's Guide to Aliasing

rachelmullen

Rachel Mullen

Posted on November 18, 2022

A Beginner's Guide to Aliasing

In college, one of my roommates and I binged the entire series Alias.

Jennifer Gardner wore hundreds of outfits, played numerous fake undercover jobs, found herself playing multiple roles, all while remaining agent Sydney Brisco.

That, my friends, is aliasing (but in this case, we'll be talking about for your data tables).

My situation: I created an app where I wanted a single user login vs. multiple logins (user, singular, vs. user & admin).

User was someone who was also a follower of events and organizations so they could be saved as user favorites. User was also an organizer of events and organizations.

Aliasing is a powerful tool to have a user be engaged in these roles as they want to be, but not as a requirement of being a user.

To help see what's going on, here are my table diagrams and the relationships.

User_id = Follower_id (user has many follows)
User_id = Organizer_id (user has many organizations and events)

Image description

Determining these relationships is a really important first step!

After determining the aliasing terms you'll use, create your tables, migrate, seed, etc.

Next, you need to alert the backend of the aliasing. You can do this by establishing your relationships in your models by telling it what are your foreign keys and what tables those belong to.

Here are all of my models to show these relationships.

Image description

Image description

Image description

Image description
(Yes, definitely taking suggestions on this User one because it makes my head spin - in hindsight, my macros could have been ever so slightly named a little differently.)

Image description

Next, let's make sure the aliasing is working!

(This is the fun part!)

Let's see who is the organizer for the first organization in our database.

Image description

It's me!

Ok, so now let's see if that user says they OWN that organization now.

Sure enough, there it is in our Rails console!

Image description

After we confirm these relationships are working on the backend, how can we implement them on the frontend?

We can use our key value pairs in React when we create a new organization.

Image description

Logged in as Rachel (user_id:1 AND organizer_id:1 through aliasing), when I submit the form as this user, we get exactly what we want on the backend!

The last organization created has a organizer id of 1, the same as our user_id!

Image description

This was my first time implementing aliasing for an app, and while I have a long way to go until I'm at Sydney Brisco's level, I'm excited for the possibilities aliasing creates in creating an organized and efficient way of managing data.

đź’– đź’Ş đź™… đźš©
rachelmullen
Rachel Mullen

Posted on November 18, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related