DEV Community

milanwinter
milanwinter

Posted on

Why ActiveRecord is Amazing

Intro

Hi there! Are you like me and have recently started your journey into the magical land of coding? Are you feeling a bit overwhelmed by how much this magical land has to offer? Are you learning to use Object Relational mapping and just get tired of writing all the same methods for each individual class? Well then you've come to the right place! I would like to introduce you to my new best friend ActiveRecord. Not only will ActiveRecord take care of those pesky repetitive methods that you've been rewriting over and over again, but it will do it with ease! You will be able to keep your code extremely DRY.(Don't repeat yourself)

What is ActiveRecord?

If you've begun learning about object-relational mapping(often referred to as ORM), then a great next step for you is to learn about and start using Active Record. If you haven't heard of it or know very little about it you can learn all about it in great depth here https://guides.rubyonrails.org/active_record_basics.html. Essentially, ActiveRecord is gem, which you can add to your code files, that is able to not only create objects and store data within a database, but also contains many built-in methods that allow you to call upon that database and perform different operations on the data.

Why is Active Record So Useful?

If you take a look at the website that I linked above and start going through the documentation, you'll notice that there are dozens of methods that you can call upon so instead of having to writing out a bunch of methods in your class that you have to rewrite for each new class that you make, you can use Active Record and omit writing many of those simpler methods and simplify other more complicated methods. For example if your code looked like this before:
ORM screenshot

Using ActiveRecord, you could make it look like this:
AR PIC

By simply making your class inherit from ActiveRecord, it adopts all those methods that are in the documentation and they now become free for you to use within your code. Look at all the empty space!
In addition, ActiveRecord doesn't just interact with your models/classes, it also interacts with your database. You can now fill up a table within a database simply by using ActiveRecord whereas before you would have to write out complicated code involving SQL to do the same thing. Similarly, once you have information within your database ActiveRecord makes it extremely easy to interact with that data and filter it down for whatever results you need.

The Methods that Make Active Record So Useful

Learning about object relational mapping may have made you frustrated because for each class you were forced to write the same methods. For example, having a reader/writer method for your arguments is pretty standard in a class as well as having a self.all method that outputs all the objects that belong to a specific class. I'm sure you'd be happy to know that ActiveRecord has build in methods for all of these! Other common methods that ActiveRecord has build in include: where, find, group, having, includes, joins, order, select and many more. These few methods are just a glimpse into what ActiveRecord can do and these methods only deal with working with the data from your database. There are CRUD(create, read, update, delete) methods all built in as well. Although the documentation for ActiveRecord is long and learning the syntax for some of these methods might take a little time to get used to, the potential time you might save by learning how to use this gem is mind boggling.

ActiveRecord must cost a fortune and take ages to install!!

You may be thinking that this is all seems too good to be true and that with such an amazing tool it will surely cost lots of money to install and use. But alas, ActiveRecord is a 100% free gem that requires some installations that aren't too lengthy. If you would like a good resource for help on installing this gem I would recommend this website: https://www.techcareerbooster.com/blog/use-activerecord-in-your-ruby-project.

Final Thoughts

All in all I think ActiveRecord is simply amazing and has already made my life easier in just the couple days that I've started to use it and my hope is that after you read this you will too. I'd also like to quickly acknowledge just how amazing the programming community is and how thankful I am that there are people out there who write these gems for everyone to use for free because without them we would be all working twice as hard.
Sources:

Top comments (0)