A Beginner's Guide to Aliasing
Tags: javascript, rails, react, ruby
Published: 2022-11-18 01:08:18

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).

[](https://media.giphy.com/media/4wuzE31HC6Lba/giphy.gif)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d1q3x8byqysss4uhfek.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmp3coa7vd44pbt6u6ey.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gv7iylb4078icq080m5w.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tw7zzadylv9ezbtxsiq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xg90bzzr7zxoazc1p9c.png)
(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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcx25pems3lavslhewms.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9noja5b24wbtk9x5p7c0.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hi7wcqxjurgowxmti9f.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5rmvuvlcoy9rjq3g20p.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u96mp10oim96u8tc41o.png)

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.

[](https://media.giphy.com/media/1U41ujsx7HDkk/giphy.gif)