- read

Maximize Your Productivity with Parent Models in Ruby on Rails 🕒

Jason Kam 34

Maximize Your Productivity with Parent Models in Ruby on Rails 🕒

Jason Kam
Level Up Coding
Published in
4 min readDec 23, 2022

--

Maximize Your Productivity with Parent Models in Ruby on Rails

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:

rails generate model Vehicle make:string model:string year:integer

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:

rails generate model Car Vehicle number_of_doors:integer
rails generate model Truck Vehicle payload_capacity:integer
rails generate model Motorcycle Vehicle engine_size:integer

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:

car = Car.new(make: "Toyota", model: "Camry", year: 2020, number_of_doors: 4)
car.save

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:

class Vehicle < ApplicationRecord
def age
Time.now.year - year
end
end

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:

car = Car.first
puts car.age

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:

class Vehicle < ApplicationRecord
def to_s
"#{make} #{model} (#{year})"
end
end

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:

class Car < Vehicle
def to_s
"#{make} #{model} (#{year}), #{number_of_doors} doors"
end
end

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!

Thinks

Writing has always been my passion, and it gives me pleasure to help and inspire people. If you have any questions, feel free to reach out!

Thank you for reading this far. Please give this article a round of applause if you can, or give me a follow. I will thank you from the bottom of my heart.