The Order Model

Generate the Order model, which will be used to update the total calculation for orders.

Generate the model

The Order model is associated with users and products simultaneously.

Thankfully, it is easy to achieve this in Rails. The tricky part is when it comes to serializing this object. We’ll talk more about this later.

We will generate the Order model using the generate model command:

$ rails generate model order user:belongs_to total:decimal

We use the references method to create the corresponding foreign key for the order to belong to a user. It also adds the belongs_to directive into the Order model. Let’s generate the model using this command in the terminal below:

Get hands-on with 1200+ tech skills courses.