Get Active!...Record
Vincent Baylon
Posted on August 18, 2021
Active Record is a Ruby gem that allows to you easily create, maintain, manipulate your database. It is an ORM (Object Relational Mapping), which means it lets you interact with data in your database as though it were a normal Ruby object.
For example if we have a dogs database, we can use Active Record to grab all our dogs with a simple:
Dog.all
. . .
Active Record Basics
-
Naming Conventions
- Pluralize the database tables associated with your class. Class name would be singular with the first letter capitalized and written in CamelCase. Table name is plural, all lowercase, and written in snake_case.
Model Class - Dog
Database Table - dogs
-
Create Active Record Models
- Subclass the ActiveRecord class. This will allow your class to inherit all the methods within the subclass.
class Dog < ActiveRecord::Base
end
-
CRUD (Create)
- Active Record creates an object and saves it to the database instead of only creating a new instance.
roscoe = Dog.create(name: "Roscoe", breed: "Mix")
-
CRUD (Read)
- Accesses data within our database. There are other read methods available here.
Dog.all
# Returns all our dogs
Dog.first
# Returns the first dog
Dog.find_by(name: "Roscoe")
# Returns the first dog named Roscoe
Dog.where(breed: "Mix")
# Returns all dogs with a breed of "Mix"
-
CRUD (Update)
- Modify an attribute then save it to the database.
albert = Dog.find_by(name: "Albert")
albert.update(name: "Alby")
# Bulk update
Dog.update_all "adopted = True"
-
CRUD (Delete)
- Remove object from the database.
alby = Dog.find_by(name: "Alby")
alby.destroy
-
Migrations
- A way to alter the structure of your database. This is stored in migration files and executed using rake.
class CreateDogs < ActiveRecord::Migration[6.0]
def change
create_table :dogs do |t|
t.string :name
t.string :breed
t.boolean :adopted
end
end
end
. . .
Active Record Advanced
-
Associations
- Sets up a connection between two models.
- Belongs_to connects each instance of the declaring class (Dog) to one instance of the other class (Owner).
- Has_many is on the opposite of the belongs_to connection and means the the declaring class (Owner) has zero or more instances connections of the other class (Dog).
class Dog < ActiveRecord::Base
belongs_to :owner
end
class Owner < ActiveRecord::Base
has_many :dogs
end
-
Pluck
- Returns an array of just the attribute you provide.
Dog.pluck(:name)
# => ["Roscoe", "Alby", "Fido"]
-
Joins
- Query one model based on data from a related table. Referencing how our Associations is set up:
Owner.joins(:dogs)
-
Scope
- Custom queries that return an ActiveRecord::Relation. You use scopes when you have a custom query that you will use in other places so you don't have to duplicate code.
class Dog < ActiveRecord::Base
scope :mix_breed, -> { where("breed = Mix") }
end
. . .
Conclusion
This was just a small sample of what Active Record can do. If you'd like to learn more I will provide some links below. Happy coding!
. . .
Resources
Active Record Basics
Active Record Associations
Active Record Query
Active Record Migrations
Scopes
💖 💪 🙅 🚩
Vincent Baylon
Posted on August 18, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.