Day 32 – Models and Queries with Active Record

Today started out with more recursive algorithms! I’m really liking recursion. The recursive versions of these old algorithms that we had been writing with for loops are coming out looking so much cleaner! For example, take a look at this recursive arraySum function:

function arraySum(arr){
if (arr.length < 1){
return 0
} else if (arr[0].constructor === Array){
return arraySum(arr[0]) + arraySum(arr.slice(1))
} else {
return arr[0] + arraySum(arr.slice(1))

view raw
hosted with ❤ by GitHub

This function will give the sum of all the elements in an array, even when the array contains nested arrays. Can’t do that with a for loop! Writing this function without recursion would mean running a while loop with variables and checks for nested arrays, which will always end up being more on the verbose side. Taking the recursive route means that there are no variables to keep track of, and nothing is changing in any scope. This is actually getting into functional programming, but I’m still not too familiar with that paradigm, so I’ll have to comment more on that later. Anyways, recursion is cool.

After morning algorithms it was time to get back to Ruby on Rails! I spent the whole day working through the Models chapter of the platform, which was all about how to use rails to generate models, generate and make migrations, rollback migrations, add associations to models in ActiveRecord model classes, and query the database in the console. It was all very reminiscent of the ORM assignments in Django, but Active Record has some pretty cool new features. Being able to generate a model with one line in the terminal is awesome. Setting up associations took a bit of finagling, but I think I’m starting to get the hang of it. Querying the database through the ORM’s model classes is pretty much the same as it was back in Django for basic stuff, but there are some weird options for doing joins and whatnot. It’s almost like taking a step back towards something more closely resembling SQL at times.

One thing I’m feeling kinda bittersweet about after today is that I’ve been doing so much stuff in the terminal. On one hand, I’m getting lots of good terminal practice, and doing stuff in the terminal is fast and feels super cool, like I’m some hacker out of a movie. On the other hand, everything I type in the terminal poofs away into nothingness and I effectively have no way of recording and seeing my progress. I’ve been saving a few terminal output files here and there, but the sheer number of lines of responses and errors and typos and whatever totally buries the few commands I care about (btw, Hirb is totally awesome for cleaning up terminal output). Yeah, I know, whenever I execute a command or query that actually works like it’s supposed to I could just stop what I’m doing to copy and paste it into some other file somewhere, but when I’m in the zone I’m not really thinking about tedious crap like that. I miss the good old days when I could iteratively set up queries mixed with print statements in my Django controllers and they’d always be right there where I wanted them to be after going and hitting the server. Oh well, I may have lost most of the history of my experimentation with queries in Rails so far, but I still have plenty more to come.

I’m looking forward to getting into controllers and views tomorrow. Once I start running the Rails server I should be able to write my queries in a controller file and save everything along the way just like how I was doing it back in Django. Then I’ll have nothing to complain about, lol. I’m also pretty curious to see what Rails does for templating and routes!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s