Breaking out into the real World

I recently started applying for positions in the software engineering field. I also just started a new Rails project. I figured it would be a great opportunity to review everything I learned once again.

I decided to create Notes 2.0. It is an application that allows a user to create a notebook that they can share amongst their classmates. The classmates will also have the opportunity to leave notes. These notebooks will also be organized by categories.

I started off by creating the database and models.

Notebook Model

The first relationship is known as belongs_to which is a singular naming convention that is added to the notebook model. Belongs_to takes in place within the models. Essentially it sets up a 1:1 convention with another model. In this case it would be a category that the notebook is going to be referencing. It will look for a category_id on the notebook’s table that matches the category_id of the record in the database elsewhere. Those id’s will match up and thats how those associations can communicate. super common associations that are always found in a rails application.

Category Model

The second most common thing that is seen in a rails application declaring a one-to-many association. In this case a category can have many notebooks. It’s the inverse of the notebook association to the category. In this case I have a category that can have many notebooks. The user model in the application should be able to create or contain many notebooks. The difference here is that there is a plural naming convention. It also indicates a one-to-many association. A category can have many notebooks. The category_id would still be the reference in this case too. The notebooks associated by the category_id. In the notebooks table in the database there would be an actual category_id column that would match up with the category_id which will be displayed in the schema.

A has_many associations can be designed with the :through option to use an explicit join model to retrieve the data. this is vert similar to the has_and_belongs_to_many association. This can be helpful as you can add validations, callbacks and extra attributes.

These are just a few of the ways in which you can use active record associations. The possibilities are endless.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store