SHARE:
Uncategorized

ActiveRecord

Flatiron School / 9 October 2013

The following is a guest post by Daniel Chang and originally appeared on his blog. Daniel is currently in the Ruby-003 class at The Flatiron School. You can follow him on Twitter here.

I took a bit of a dive today and looked a Ruby on Rails presentation. Here’s what I gleaned from looking through all the slides.

  • Rails is a web application framework that allows your Ruby to interact with the web seamlessly. By “interact with the web”, I assume the presentation means “interact with databases.”

  • Rails implements a principle called “MVC” — Model View Controller. Rails seems to handle each task separately. I’m going to focus on the Model, which uses ActiveRecord.

  • ActiveRecord is a Module that Rails uses to model and interact with databases. It makes database queries both simple and intuitive.

Here are some concrete query comparisons between using SQLite3 versus Rails’ ActiveRecord.

Assume we have a table called “dogs,” each with a name, age, weight, and type. Each attribute would be a column, and each row would represent a different dog.

We can also imagine this as a class of Dog objects in Ruby. Here’s how one might create the barebones structure in Ruby.

See how simple that was? Now we can make queries on the table.

That’s just the beginning! Check this out.

It seems like ActiveRecord must use certain keywords like ‘by’ and ‘and’ in order to dynamically use the attribute names that the user inputs.

What is really beautiful though is the idea of “configuration of convention” that Rails implements in order to shorten the amount of code you need to write. Slides 42-44 of the presentation gave a particularly good example of the idea. No wonder people love Rails!

The slides continue talking about the MVC

The End of Fun Previous Post Testing with Rspec, or Errors as Hints Next Post