Introduction

Active Record is used in Ruby for Object Relational Mapping (ORM). This means that it connects Ruby with a database without having to write out SQL statements. In MVC it is the M which is the model which handles the data side.

Active Record gives us the ability to:

  • Represent models and their data, basic class definitions of object.
  • Represent associations between these models, provides associations like has_many, belongs_to, etc.
  • Represent inheritance hierarchies through related models, where models can inherits attributes/data from another model.
  • Validate models before they get stored to the database, check if attributes matches conditions like presence, length, uniqueness, etc.
  • Perform database operations in an object-oriented fashion, like accessing joiners through methods.

Conventions

Naming convention for models is camel case but the first letter is capitalized as well. For example, User, Student, CartItem, etc. For schema it is snake case where every letter is lower case and words are separated by an underscore and should be pluralized. For example, users, students, cart_items, etc. Inside the schemas foreign keys are snake case and the name should reference the table name. For example, user_id, student_id, cart_item_id, etc. The “_id” is necessary because Active Record uses these naming conventions. Primary keys are integers by default and will be automatically created by Active Record.

Active Record Models

The class should inherit from ApplicationRecord. The file should go in the models folder.

The CartItem model will be mapped to the cart_item database table. To create the table the file should go in the db/migrate folder. You can also run the cmd “rails generate model CartItem name:string” which would create the model and the migration file for you.

After creating the migration file you can run the command “rails db:migrate” to create the table. If it worked you should be able to see in the schema.rb file in the db folder something like this:

CRUD

To make new data and to save it you would call the create method and pass the attributes to create the object and automatically save it into the db. If you use the new method it will create the object but not save it into the db, to save it you would call the save method.

To read the you would call the class itself and use the Active Record methods.

To update you would get the object modify it and then save it.

To delete you would find the object and call the destroy method. There are other variations of destroy as well.

Seed

When testing applications you want test data but sometimes you want to clear all the data as well. To create test data without having to do type out everything constantly you can create a seed file in the db folder.

To run the commands you use “rails db:seed”.

Written by

Hi

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