Active Record

What is it?

Active Record is the M in MVC(model, view, controller). MVC is a architectural pattern to create and update user interfaces. The model updates the view, the view displays to the user, the user uses the controller, and the controller manipulates the model.

Active record is the layer of this architecture that takes care of business data and logic. It acts as a database management system for Rails.

When using Active Record, make sure to remember and all important CRUD.

Create

If you have had any prior experience with SQL and/or other databases, you would recognize this instantly. Create command allows objects to be created from a hash. The process of creating tables in Active Record is identical to SQL.

Read

Read is how you access information from your Active Record database. Since Active Record is written for Rails applications, you can use some unique data access methods to quickly retreive your data.

.all returns a collection of all users.

.first returns the first user.

.find_by(object:'attribute') returns the first attribute by searching the particular Active Record object.

Update

Update allows you to modify attributes. This command is used after you retrieve a particular user via Read.

Ex:

To update records in bulk, use the method .update_all

Delete

Delete is self-explanatory. Allows you to destroy Active Record users/objects after they have been selected by Read via the .destroy command.

Resources

For further research, check out the Resources listed!

Ruby On Rails Guide CodeIgniter