What I’ve Learned in Boat(Cod)ing School is…

What I’ve Learned in Boat(Cod)ing School is…Matthew PlautBlockedUnblockFollowFollowingMar 7Hopefully more than Spongebob!Entering day one at Flatiron School, I had a solid foundation in Ruby.

I had learned arrays and hashes and iterations, and etc.

Day one started my journey deeper into the depths of Ruby.

I learned about classes and why they are so useful (spoiler alert: they help you encapsulate much more functionality).

By creating a class, with methods inside, you can operate on an object of that class much easier.

class Blog#code that is accessible to Blog class objectsend #example of a classNext came class methods vs.

instance methods.

A class method is one that is accessible by the class itself, whereas an instance method is only accessible to the instance of the class you are operating on.

For example:class Blogdef Blog.

all#example of a class method that returns all instances of Blog classend def get_name_of_blog#example of instance method that returns blog name of an instanceend endAfter getting a grasp on classes, next came relationships between classes.

These relationships fell largely into three categories: One-to-Many, Many-to-Many, and Has-Many-Through.

A one-to-many relationship is one where one class belongs to another class.

For example, a blog post belongs to a blog.

On the flip side, a blog can have many blog posts, hence the one-to-many relationship.

A many-to-many relationship can best be described with an example.

A person can use many social media platforms and each platform can have many people on it.

So a Person class has many Platforms and a Platform class has many Persons.

Last, and perhaps the most useful relationship, is a has many through relationship.

This is very similar to the many to many relationship, except here there is a third class that joins the other two.

For example, a Student (like Spongebob!) has many Instructors (poor Mrs.

Puff needs help) through Boating Tests, and an Instructor has many Students through Boating Tests.

Source: https://learn.

co/tracks/web-development-immersive-2-0-module-one/quizzes/oo-ruby/ruby-boating-schoolThe benefit of a has-many-through relationship is that the two classes on the outer edges can more easily access each other through the join class.

Great, now I know about classes and relationships in Ruby.

Now what?All of this is useful, but what if I want to create an app and save some information to it.

Ruby gives us no inherent way, so this brings us to SQL (Structured Query Language).

SQL is a programming language that interacts with and uses databases.

Its syntax is much different from Ruby.

For example, creating a table for a database in SQL looks like this:CREATE TABLE blogs ( id INTEGER PRIMARY KEY title TEXT author TEXT);The code looks very different from what we see in Ruby, but this is what allows us to save data we may want to access in whatever we may want to build.

Yay!But wait!.How can Ruby and SQL interact if they are not the same language?.That was a question I first had when I started learning about SQL, and it was answered with ORM (Object Relational Mapping).

ORM essentially maps our Ruby program to an SQL database, allowing our ruby classes and instances and objects to be saved in an SQL database.

Ok great, but this stills requires us to write a whole lot of code.

Could there possibly be an easier way for us to do this??Squidward is such a pessimist…Yes!!.Don’t listen to Squidward, there is an easier way.

Enter ActiveRecord.

ActiveRecord is this seemingly magic coding “tool” that is used for linking Ruby and SQL and creating class relationships with far less code than you would even think is possible.

I couldn’t believe how amazing it when I realized how much functionality it had.

Two thumbs up!!Spongebob must’ve just found out about ActiveRecord’s amazing functionalityHere’s example of how ActiveRecord simplifies things immensely:The above has three classes Student , Instructor , and BoatingTest that inherit from ActiveRecord.

They represent a has-many-through relationship.

A student has many instructors through Boating Tests and an instructor has many students through Boating Tests.

A student may have many Boating Tests (especially Spongebob!) and an Instructor may have many Boating Tests, but each test is a unique object that joins a student and an instructor.

What makes ActiveRecord awesome (in part) is that without it, to map these classes to each other, we would need to make a bunch of other methods and a lot of code.

Here, the ActiveRecord macros (a series of built in methods) has_many, has_many through:, and belongs_to do the work for us.

Amazing, right???.Guess I’m all done with learning how to code.

Nothing else to do!Wrong!!.I’ve only begun to scratch the surface of ActiveRecord and I’m just beginning my coding journey.

Catch ya on the flip!.

. More details

Leave a Reply