Wednesday, August 31, 2011

Alphabetical sort method in Rails

So the other day, frustrated with the complete lack of progress I'd made on trying to figure out how to link to a Background model from a Character's view, I decided the best thing to do is to work on some smaller problems until I either ran across a method that seemed like it would work, or INSPIRATO struck me, or I felt like battling that wily Background again.

One of the things I wanted to do is make it so when a user creates a list of Skills, they're displayed in alphabetical order. Sounds simple, right? And it is! Absolutely. But I had only the vaguest idea of how to write such a method (probably finding all the skills, then ordering them by name) and absolutely no idea how to call that method in the view.

So I headed over to Stack Overflow since this seemed like an easy enough problem that I could describe and get answered quickly enough. Who came riding over the hill like Gandalf, ready to save the day? normalocity! I have 0 idea who this guy is, but his method was clear, easy to understand and exactly what I was looking for. Have I mentioned how awesome the Rails community is, both locally and online? One of my favorite things about my learning process has been getting to know more people, and talking to more experienced programmers about Rails, coding and how things work in general.

So here's what I ended up doing. I first had to define the method in my skills_controller (which I had already done, and correctly too!) and then call that method in the view.

So here's the definition I came up with:

def index
  @character = Character.find(params[:character_id])
  @skill = @character.skills.build
  @sorted_skills = @character.skills.find(:all, :order => :name)
end

Not bad! But then I got stuck. How do I call it in the view? Is it a separate thing? I've already got code that iterates over each skill and then spits it back out to the view. So was it a second call? That doesn't seem logical. So what about editing the code I already have, and calling the new method I wrote instead of the previous method?

Just one little change here. We started off with:

<% @character.skills.reject {|skill| skill.new_record? }.each do |skill| %>

And changed it to:

<% @sorted_skills.reject {|skill| skill.new_record? }.each do |skill| %>

So this code is doing the same thing it did before, but instead of simply bringing up @character.skills, it's bringing up @sorted_skills, which has already been defined as @character.skills.find(:all, :order => :name.

And this is just one example. I've got a crazy idea for a way to sort by two variables. For example, a Character will have class and cross-class skills. What about a way to sort these skills both alphabetically and by class or cross class skills? Nutty, I know! I'm letting that one brew for a while, though. Or what about spells? It might make sense to sort spells both by spell level and alphabetically. But you see what I mean? It's kind of getting impossible for me to learn something new in Rails without a) wondering how else I can apply it and b) wondering how I can tweak it, change it, expand it, pose it, scroll it, click it, or zoom it.

It felt really good to figure this out tonight, and even though this one instance is just a tiny fix that literally took two seconds to code, the logic behind it and understanding that logic reaches quite a bit deeper. After all, I'm not learning Rails to build Dungeons and Dragons character databases. I'm learning Rails to understand Rails.

Tuesday, August 30, 2011

Heroku and missing migrations

So my goal today was to dust off my Heroku account, throw up Chorenivore and Manticore, and then do a bit of coding. HOWEVER! Somehow a bunch of my migrations in my Manticore app were deleted, so getting rake commands to run on Heroku took a bit of finagling. And when I say finagling, I mean looking at my schema, rewriting the migrations and then applying them. Not a bad way to handle a migration that's gone MIA. I'm not entirely sure how this happened, but it's been fixed. This development process hasn't been the smoothest thing I've ever done.

Do you ever have this happen? You're looking at an older side project, maybe feeling a few pangs of nostalgia? Like "Oh, Chorenivore, I was so in love with you back in May! Where did we go wrong?"

I'm guessing probably. Or are programmers like sharks and can't look back?

Anyway, here's a link to my Heroku deployment of Chorenivore, my to-do list application:

Chorenivore

A bit rough, sure! But I don't hate it. I kind of want to go back and work on some things I wanted to implement, but maybe some time in the future.

And here's Manticore, my Dungeons and Dragons character database application:

Manticore

I also ordered a copy of this book and can't wait for it to arrive:


I love the PragProg team and I think this will be another great resource for me.