Scaffolding on Crack

Do you have a massive list of items in a scaffold-type arrangement (e.g. in a typo-style admin)? Here’s a way to quickly and easily filter and sort your big-assed list.

Note: this requires the Hash#to_sql function posted here a few weeks ago .. told you it was useful!

More after the jump..

First, we’re going to tackle filtering; if y’all want sorting or ajax, too, please leave some comments to motivate me to write the second half. Just so I know you care :)

Like any good web coder (har, har) let’s start with the view. We have articles, they’re organized into simple categories (Category model), written by authors (Author.find(:all) is stored in @authors)

<th>Post title</th>
<th><select name=’main_category’ onchange=’filtering();’ id=’filter_category’>
<option value=”>Main Category</option>
<%= options_from_collection_for_select(Category.find(:all), ‘permalink’, ‘category_name’, params[:category].to_i)%>
<select name=’author’ onchange=’filtering();’ id=’filter_author’>
<option value=”>Author</option>
<%= options_from_collection_for_select(Author.find(:all), ‘id’, ‘nickname’, params[:author].to_i )%>

Now, that’s the header.. it looks something like this:

Post title   Main Category   Monkeys   Elephants   Author   Joe   Fred
The giant monkey returns Monkeys Joe
Who’s the biggest, grayest mofo? Elephants Fred

Finally, some pagination:

<div class=”pagination”>

<% @pagination_params = {
:category => params[:category],
:author   => params[:author],
:action   => :list,
:controller => ‘article’
<%= pagination_links(
@content_pages, :window_size => 15,
:link_to_current_page => false,
:params => @pagination_params


Now, you may notice there’s some javascript in those Select boxes: onchange=’filtering();’. Here’s that function.

<script type=’text/javascript’>
function filtering() {
var url = “/article/list/?filter”
var category = $(‘filter_category’);
var author = $(‘filter_author’);

if (category.selectedIndex > 0) url += ‘&category=’ + category.options[category.selectedIndex].value;
if (author.selectedIndex > 0)   url += ‘&author=’ + author.options[author.selectedIndex].value;
window.location.href = url;

Hopefully that’s fairly self-explanatory; it builds a query string from the values of the two select boxes. Next: onto the controller.

class ArticleController < ApplicationController
def list
# create a conditions hash from our params, if they exist
@conditions[:main_category_id] = Category.find_by_permalink(params[:category]).id if params[:category] and !params[:category].empty?
@conditions[:user_id] = params[:author] if params[:author] and !params[:author].empty?

@content_pages = self, Article.count, 10, @params['page'].to_i
@contents = Article.find(:all, :conditions => @conditions.to_sql, :order => ‘published_at desc’, :offset => @params['page'].to_i, :limit => 10)

@authors = Author.find_by_sql(“SELECT DISTINCT(content.user_id), users.* FROM #{Article.table_name} LEFT JOIN users ON content.user_id = WHERE content.type = ‘Article’ ORDER BY users.nickname ASC”)
# this is our fairly specific sql, yours is probably much different.

@categories = Category.find(:all)
@authors = Author.find(:all)


Voila! We now have dropdown filtering.

The 3 Coolest Ruby on Rails Projects

As you know, I’m a bit of a fiend for all things Ruby on Rails. I love the language, and how incredibly versatile it is. It’s such a powerful tool, and today I want to showcase to you some of the amazing things people all around the world are using it for.

First up is the very cool Tennis Hub. The founder, Eddie Ross, is a super smart guy who has been in the start-up scene for a while now. He is also an avid tennis player, and saw a whole in the market.

Tennis players were only playing a fraction of the amount they wanted to play, simply because they couldn’t find other people to play with often enough. Tennis coaches had plenty of sessions they wanted to full, and tennis clubs were operating well below their capacity.

So with a team of Ruby developers, he went about creating a platform that would connect tennis players with other enthusiasts, with coaches and clubs, so that everyone in the tennis community would see an upturn in their involvement, and for coaches and clubs, in their profitability.

You can think of Tennis Hub as a platform like Tindr. Someoen posts availability in your area, you click to say you’re in, and it’s a done deal. They only charge coaches and clubs $2 per booking, but it can add a tonne of revenue to their annual P&L sheet.

I love a good game of tennis myself, so I’m thrilled for Tennis Hub to keep expanding until it gets to my area and I can start taking advantage of it.

Next up is SupaDupa. It’s basically a ‘plug and play’ platform for boutique stores. It comes with customisable themes and templates, and frees small creative businesses to really take their store to the next level.

Since a few of my friends are the fashion-y types, this makes me really excited. There’s finally a crossover in our interests! It’s got a really great UI, making it so simple for even the most technophobic types to create an amazing ecommerce platform.

Finally, comes RiskMNGT, an incredibly powerful tool for businesses in high-risk niches, particularly in software and development. RiskMNGT essentially allows you to predict and manage risk according to a pre-determined set of parameters.

You can track and manage everything from impact, to probability, to exposure level, as well as developing mitigation and contingency plans. It’s pretty damn impressive and will be saving a lot of heads from a lot of chopping blocks.

The Best Music To Code To

I don’t know about you, but the music I rock out to while I’m programming has a big impact on how I work.

If I’m listening to heavy, hard rock stuff, I find myself banging the keys on my (already battered) keyboard way too hard, feeling unstoppable but actually making a lot more mistakes than I should be.

If I go with trippy stuff, like Phish or MGMT, I float along, feeling zen, just flowing with the work at a comfortable pace, with only an occasional mental glitch.

Sometimes I’ll get nostalgic and whack on some Fall Out Boy, or Dashboard Confessional. The old familiarity just gets me in the zone instantly and I’m on fire for as long as the album lasts.

Other days it’s a dubstep kinda feel, and I find myself wriggling around my seat a lot more than I would usually. Those days I feel super energised about my code and I go to bed really satisfied.

Unfortunately, since every day is different and brings different challenges, I can’t just settle on one style to go with every day.

For this reason, I’ve created huge playlists that move me through each day, regardless of how I’m feeling or how the programming is coming. I made them carefully, intentionally listing tracks to move them naturally from one genre to another so I can get in a flow and not be distracted by a jump from dubstep to quiet classical.

This came up amongst some friends the other night and it was awesome hearing about bands and artists they are listening to. It gave me heaps of new material, so I encourage you to get out there are find some fresh stuff too!


Health Tips For Programmers

Let’s face it. Ever since you started coding, your body has taken an unmistakable turn for the worse.

Your skin has lightened to the point of being reflective, your diet now consists of crisps and cookies, and let’s not even talk about the disaster zone that is your posture.

Actually, let’s.

Your spine. You are ruining it. And you might not have noticed lately, what with all the non-movement, but you need your spine for a few things. You know, walking, standing, not being a cripple.

So here’s what you’re going to do.

Set yourself an alarm, every hour on the hour throughout your workday. Every time that alarm goes off, you are going to stand up and walk away from your desk. You’re going to swing your arms around and bend over a few times. You’re going to roll your shoulders back and forth, and make sure that you’re standing straight.

You’re also going to go to Massage World, and order yourself a basic table. When it arrives, you’re going to get a recommendation for a reputable massage therapist, and set up a monthly appointment with them where they will come and annihilate all the knots in your muscles and straighten out your skeletal alignment.

Finally, you’re going to stop drinking all that corrosive rubbish that burns your stomach and rots your teeth and replace it with water. Yes, water. I know you rely on your sodas for energy but you will die an early and unpleasant death if you don’t get this in hand.

Got it? Good. Get to it.

How To Have A Social Life If You’re A Coder

Ah, code. For most of us who work with you, you are an infuriating, impossible, irresistible lover we just can’t get enough of.

You keep us up at night. You keep us in at home when we know we should be out. Time and again we choose you over older, less demanding friends.

So it’s little surprise when, a few years into the relationship, we claw our way up to air to find our social lives in tatters.

Today it’s time to find a little balance in the relationship.

First things first. Your friends. You should contact them. You might have to remind them who you are, and how you know them. Once that’s taken care of, you should invite them to meet up with you for a few quiet drinks.

You don’t want to overwhelm them by excitedly announcing that you’re going to have a rager. Plus, you haven’t been out in ages. You can’t handle a rager, so don’t put it on the table.

Once that’s done, set yourself reminders. This ain’t gonna be easy. You need to be strict here. Set reminders on your phone and computer 40 minutes and 30 minutes before you need to leave.

That way, you have time to finish off that string you’re working on, or at least scribble down where you’re at with at, as well as leaving time for a shower and a sprucing. No, seriously. You need to shower. You look terrible, dude, you can’t go out like that.

Finally, get to the venue and stay there for as much beer and banter as you can manage. Preferably a bit more. Be interested in your friends, and do not for the love of God tell them about the problematic jQuery you’re dealing with right now.

Just relax into it. Have some fun. Remember why people are just as good company as your computer, and try to organise the next time you’re going to do this, too.

A Brief Introduction to Ruby On Rails

Ruby on Rails is the new kid on the block when it comes to programming. The other languages – PHP, Perl, Java and so on have been around for many years. Ruby’s only about 5 years old, having been extracted from Basecamp’s sophisticated programming by David Heinemeier Hansson.

Now, while other folks refer to it as Rails, around here, things go Ruby’s way, so don’t go getting confused if you hear someone else calling it differently.

Ruby is built on a pattern based on models, views and controllers.

The models are the part of the app that hold all the data. They describe the problem the app is trying to fix, as well as giving the app a structure to do that in.

The views are appearance templates that render data so the user can work with them, as well as managing all the visual and representational logic in the app.

Finally, the controller is the man in the middle, processing requests from users at the view point through to the model in the back end. It controls the flow of information and process in both directions of the app.

When Heinemeier Hansson extracted this programming methodology, he did it with a view to increasing programmer productivity – not app performance. This means it’s really not everyone’s cup of tea… meaning that the number crunchers don’t love it because it’s not necessarily going to give them the quickest return, but programmers and devs love it because it’s so much better to work in than the older, clunkier languages.

Yes, it’s a bit slower than those titans, but it’s getting faster and faster with every iteration and I think the programming community is only going to grow to love it more and more.

What Is HABTM?

Real talk. HABTM means HasAndBelongsToMany, but it’s one of those confusing acronyms that doesn’t really match what it stands for.

See, when you Wiki it, the page that comes up is Many-To-Many. Which is fine, it makes sense, but it’s not matching. I kinda like when things match, ya know?

When you’ve been programming a while, you get used to hacking together HABTM or many-to-many commands. These two rays of sunshine are basically a way of modelling data, or showing a correlation between database tables.

Database tables are built around cardinality, which is essentially a very precise description of why those tables should be linked, and the logic that keeps them linking together accurately.

Now, many-to-many tabling is pretty fancy, but to to make it work you also need to understand one-to-many tabling. To stick with the excellent example given in Wikipedia on cardinality (this table association):

A hospital database might contain 3 different ‘tables’ or data sets that need to be linked together: the table of doctors working there, the patients being treated, and the departments that make up the hospital.

In a one-to-many situation, the doctors and departments might be linked, as each doctor would only operate in one department (so for example, you’re not going to find the oncologists showing up in the pediatrics table).

But in a many-to-many situation, you might find the crossover between multiple patients being treated by multiple doctors – and sometimes even in multiple departments.

This crossover is where understanding HABTM really counts. You can’t be having these table interactions get messed up or the database is going to spit out completely screwy data. Getting familiar with HABTM takes time but it’s worth putting in the effort to get it right, because it’s going to open many, many doors for you. Get your tabling accurate and you’ve got a multitude more projects you can work on.