Sorting and Ordering in Rails

Example 1: Writing your own sort routine

I originally tried ‘sort_by’, but that only works for existing attributes and apparently in only one direction.

My ‘is_default’ attribute is setup as a boolean and is almost always false, except where I define an image as being the default for a given product.

Converting a boolean to a string and then sorting alphabetically in reverse order is not something I liked nor recommend, but it was the first thing that I got working.

The Admin helper:

Quick and Dirty Guide to Deploying Rails with Capistrano

Capistrano is one of those problem solving apps that can be tricky to setup at first, but is a no-brainer once you’ve deployed with it.

I added a few notes about hosting on Dreamhost because they only support rails 2.2.2 at the time of this writing. My current app is actually developed under 2.3.2.

With Dreamhost, you only get a RAILS_ENV=’production’ setup on Passenger hosting…but that is good enough. I prefer to run test.the-app.com locally anyway.

If an app of mine gets popular, I would move it off of dreamhost regardless ( security, reliability, etc ). Try slicehost or linode if you’re looking for a more professional web hosting solution.

Sleeping Between API Requests

I spent a few minutes today debugging the reason why one of my “rake” tasks was getting 503 connections (Service Unavailable) from Yahoo’s API.

I had mistakenly thought this was due to the fact that Yahoo limits the number of requests to their API in a given day. I decided to put a “sleep” call in my code to be sure it wasn’t being denied access due to server-side throttling. Sure enough — all requests succeeded after adding a brief call to the “sleep” function.

Things I’ve Learned from Ruby on Rails

Start deploying your application immediately…even if it is just from your local development environment to a QA environment. You’ll save yourself the headache of having to learn all that when you’re ready to release to production.

Always use source control, like subversion or git. This is a no-brainer, but people still ask why. You’ll be happier when you start deploying with revision control in place.

Always backup your files and databases — another silly mistake often overlooked.

MVC (Model View Controller) is a great way to ensure maintainability on a progressively complex web application project.