Maximize Your Productivity with Parent Models in Ruby on Rails

Ruby Parent and Child

Let’s say you are building a web application that allows users to store information about their vehicles, including cars, trucks, and motorcycles. You decide to create a parent model called “Vehicle” that includes basic information about all types of vehicles, such as the make, model, and year. You can create this model using the rails generate model command:

This will create a new model called “Vehicle” with three fields: “make,” “model,” and “year.” You can then add any additional fields or validations to the model as needed.

Next, you can create child models for each type of vehicle that you want to store information about. For example, you might create a “Car” model for storing information about cars, a “Truck” model for storing information about trucks, and a “Motorcycle” model for storing information about motorcycles. To create these child models, you can use the rails generate model command followed by the name of the child model and the name of the parent model:

These commands will create new models called “Car,” “Truck,” and “Motorcycle,” respectively, and they will all inherit the fields from the “Vehicle” model. The “Car” model will also have an additional field called “number_of_doors,” the “Truck” model will have an additional field called “payload_capacity,” and the “Motorcycle” model will have an additional field called “engine_size.”

Once you have created your models, you can use them to store and retrieve information about vehicles in your application. For example, you might use the following code to create a new car object and save it to the database:

This code would create a new “Car” object with the specified make, model, year, and number of doors, and it would save the object to the database. Because the “Car” model inherits from the “Vehicle” model, it will also include the “make,” “model,” and “year” fields from the “Vehicle” model.

You can also define methods on your models to perform various tasks. For example, you might want to define a method on the “Vehicle” model that calculates the age of the vehicle based on its year. You could do this by adding the following method to the “Vehicle” model:

This method calculates the age of the vehicle by subtracting the year it was made from the current year. You can then call this method on any object that is an instance of the “Vehicle” model or any of its child models, such as a “Car” or “Truck.” For example:

This code would retrieve the first car object from the database and then output the age of the car by calling the age method on the object.

You can also override methods that are defined in the parent model in the child model. For example, let’s say you want to define a method on the “Vehicle” model that returns a string representation of the vehicle, such as “Toyota Camry (2020)”. You could do this by adding the following method to the “Vehicle” model:

This method returns a string that includes the make, model, and year of the vehicle.

Now, let’s say you want to define a similar method for the “Car” model, but you want to include the number of doors in the string representation. You can override the to_s method in the "Car" model like this:

This method is identical to the to_s method in the "Vehicle" model, but it includes the number of doors in the string representation. When you call the to_s method on a "Car" object, it will use the override method in the "Car" model instead of the method in the "Vehicle" model.

Using parent models and methods in Ruby on Rails can help you to organize and structure your application more efficiently, and it can make it easier to maintain your application as you can make changes to the parent model and have those changes automatically propagated to all of the child models. I hope this additional example helps to illustrate how parent models and methods work in Ruby on Rails!

--

--

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