Adding References In Rails Using The Command Line

Adding References In Rails Using The Command LineJake BartlowBlockedUnblockFollowFollowingMay 2There are a few different ways to unlock the power of Active Record associations directly from your command line.

This is a short breakdown of what those options are and how they differ (or don’t).

The example I’ll be using is the relationship between Doctors and Appointments (doctor has_many appointments, appointment belongs_to doctor) and it is a pre-existing relationship, meaning I’ve already built my Doctor and Appointment models.

No matter what, in order to add the reference to this preexisting relationship, you’ll need to add a migration.

The most common step you would use to add this migration through your command line is the following:So your are telling the command line that you want “rails to generate a new migration, adding the doctor reference to the appointments table.

Please not that writing ‘doctor:belongs_to’ is the exact same thing as ‘doctor_references’.

New versions of Rails have gone through several different iterations of what the generated migration looks like, but I’ll focus solely on what this looks like in 2019:If you are just starting to learn rails, this may not look completely familiar to you (it didn’t to me).

Using our example, my initial understanding of the AR association was founded on believing that a ‘doctor_id’ column needed to be included in the ‘appointments’ table, and that’s not exactly what we’re seeing above.

However, I can see how it could make sense.

We are: “adding a reference to doctor in appointments and foreign_key (doctor_id) is true.

”Let’s see what our schema looks like after we ‘rails db:migrate’:Okay, this makes a lot more sense, right?.We can see that ‘t.

integer “doctor_id”’ in our appointments table, which is what we wanted.

What we might not have expected was this additional piece:Before going any further here, I should say that we’ve already accomplished our goal.

If you are just beginning Rails, you can ignore that ‘t.

index’ and move forward confidently, knowing that you will have that doctor_id column in your appointments table.

Alternatively, we could forget about all that we’ve done previously and simply write this in our command line:This will give us the same schema as what we have before, without that additional ‘t.

index’ and would be entirely sufficient for a very basic Rails app.

However, if you are a curious person or further along in your Rails journey, you should definitely know why Rails automatically adds that index to the foreign key when you generate a reference.

While researching for this post, I found that these folks provided the clearest example of the value that indexes provide:Indexes give SQL its own reference for where to start its search, exponentially decreasing the number of records it needs to comb through and conversely increasing the speed in which it can return a query.

So while you do have the option to simply migrate over that doctor_id as a column, you should consider starting to use ‘references’ or ‘belongs_to’ now so you are ready when you take on more data-hungry Rails apps.

.

. More details

Leave a Reply