Articles about Rails

Active Record STI and eager loading

This week has been … interesting. Not because of obvious geopolitical events, but because a client application started throwing weird errors; background processes failed in ways that weren’t possible and otherwise reliable cronjobs just didn’t do what they were supposed to; no failures or anything, they just didn’t process the records they were meant to.

SimpleLocalize on Rails

When your application spans across multiple countries and languages, managing your translations using Rails’ default locale files in git becomes too cumbersome. We recently moved a clients Rails app to SimpleLocalize to give translators and developers a better workflow around translations. This is how we did it.

Ember on Rails: Validations

In the previous episodes we’ve built a working Ember.js application to manage our growing collection of Books. It lives inside a Rails application which it uses for its backend.

We’re able to list all the books in our vast library, and also add new books to our collection. However, if we try to add a book without giving it a title, the backend gladly accepts it and saves it, which is probably not the optimal behavior.

It is time to add validations to our application.

Ember on Rails

I’ve been trying to get into Ember.js a few times by now, but I’ve always been dissuaded for various reasons.

This time around I’d like you all to learn with me - hopefully the act of me writing down what I do will make it stick better for me.

The plan here is to end up with an Ember application backed by a Rails JSON API - we’ll see if the future agrees with that goal.

Heroku, Rails 3, and Sass

Heroku is a great way to host your Rails apps. Rails 3 is the new hotness. Sass is a decent way to write your stylesheets. Unfortunately combining the three isn’t as straight forward as it could be, but it can be done.

Update: While these instructions are still correct, you should use the Hassle fork from jasoncodes instead of my fork. Alternatively, you can roll your own following Herokus instructions.

Find missing translations in your Rails application

I am currently wrapping up a client-project where I am preparing a Rails application for internationalization. The application is currently in English and I am translating it to Danish as a proof of concept.

I am using the I18n::Simple backend and the resulting Danish locale file is now at 2100 lines. That’s a bit much for me to keep in my head at once, so I created a small Rake task that takes all locales in your Rails application and reports back all the keys that are missing from one or more locales. Works wonders for finding those typos or oversights that would otherwise result in ‘missing translation data’ messages.

Rails Camp DK 2008

Take 12 attendees (one from Italy, way to go, Fransesco!), 11 Apple laptops (and one Windows one, I believe), 1 wifi hotspot, mix it all together in a cottage in the middle of nowhere, sprinkle it with a tad of Ruby, Git, and *jour and let is stew for 2 days and 2 nights.

What you get is a Rails Camp, and the above recipe was cooked with great success last weekend. Thomas Watson has a bunch of pictures from the event.

Rails Rumble 2008

Rails Rumble 2008 is in full effect. Rails Rumble is a 48 hour Rails development contest, and I and two others from the Copenhagen Ruby Brigade have decided to enter the fray.

2 hours to go

The time is getting close to midnight and my eyelids are getting to close to the floor – or so it feels. We’re done, there’s no way we can manage any more improvements.

The last hour we’ve been doing bugfixes by deleting the stuff that’s not working. When something is buggy with no time (or energy) to fix it, and you refuse to deploy low quality stuff, that’s the way to go about it. Oh well.

Rails Rumble 2008 is over for us, looking forward to going through all the apps – some of them are looking really amazing.

3.5 hours to go

Fatigue is definitely setting in now. We’re doing stupid mistakes and things aren’t moving nearly as quickly as it has been. But, we’ve been able to check off some of the items on the “nice to have” list, which is… well… nice. Looks like my ability to blog doesn’t increase the more tired I get, either.

Anyways, the site has been deployed, and we don’t want to risk doing anything major to it for the remainder of the contest – we’re bound to break something if we do.

So without further ado I give you Quotagious – the most awesome way to store and find quotes.

7 hours to go

Uh oh, less than a regular day of work left. Actually, a lot less as it’s highly unlikely we’ll stay awake until the deadline, it being at 2AM our time and tomorrow being a work day.

Funny how tons of small things crop up as we get closer to the deadline. Luckily we have the features we aimed for done. And they would’ve been deployed had it not been for the fact that something took the server down. Oh well, with no staging environment stuff like that happens.

Thomas has been working on Twitter integration for the last few hours. We didn’t want to be the only Rails Rumble app not hooked up to Twitter somehow… Performance optimizations and even more usability improvements have been my focus for the last few hours – our beta testers are ruthless, thanks guys :)

In related news, we’re now down to 99% Ruby code, 1% Javascript :(

12 hours to go

75% through the competition. So far I have been focused on usability and making the site look nice. Thomas has started on one of the fancier features, and my wife has been doing beta-testing for us.

Having someone outside the team look over the application is invaluable, even if done remotely. We’ve already implemented some changes, primarily in relation to wording and program flow in order to make it clearer what the application is and how it works.

The application is pretty solid, and we’ve added a “stable” tag to it. So if we end up messing it totally up the last twelve hours, at least we’ll have something to deploy. We’ve also started working purely in feature branches (yay Git) to lessen the risk that we end up deploying something half-implemented and broken.

Day 2 – 17 hours to go

Good morning, time to dive back into the code after a short nights sleep. Unfortunately, the more interesting stuff will have to wait, as the nights beta-testing revealed a few serious issues that needs to be fixed; primarily usability-wise. I have a feeling that we won’t get many favourable votes if people cannot sign up or log in.

26 hours to go

Yawn, tiredness is definitely setting in, but we’ve come a long way by now. The core functionality is working and seems solid – although I am sure it’s all an illusion that shatters the moment a real user gets involved. We’ve even got around to adding the majority of the UI theme.

UI-wise we had originally decided on just buying a stock theme somewhere, but we ended up doing it ourselves. I had some visual ideas I wanted to try out, and most the themes we could find in the short time we’d put aside for it, didn’t feel right and/or would need too much modification after the fact. So we’re rolling our now – support for IE6? Yeah, right.

For the last couple of hours we’ve been mainly doing minor stuff, none of us really feel like tackling the bigger ToDo items. Looking at the bright side, this means we’ve got a fairly complete’ish application now not even halfways through the competion.

The plan for tomorrow is to dig into the more interesting stuff; the features that move the application away from “meh, this is just a script/generate scaffold job” and into Championship belt status – or at least, that’s the plan.

The plan for right now: Sleeping.

32 hours to go

The first long workday (and then some) has passed – and we’re still going strong. However fatigue is becoming noticeable and I suspect we’ll be heading away from the computers to do something different very soon.

The application is really shaping up. We’re dealing with a bunch of fairly intricate edge cases we discover as we learn more about data model, and Test Driven Development is really helping us out here.

Our To Do list for today is growing thin, and it looks like we might be able to start working on polish and cool stuff already today.

37 hours to go

5 hours in, and we’ve started questioning our data model. It’s not even that it’s that complex. We’ve got 5 models, but it should probably be just 4. Too bad we can’t figure out if it’ll make things easier or harder cutting the one model.

Another thing we’ve been running into is the desire to make things too good. When you’re facing a 48 hour deadline you have to settle for good enough. There’s simply no time to go “Hrm, that URL doesn’t like exactly like I’d like it to, let’s just tweak it a liiiiiiittle bit…” – before you know it, you look at the clock and it’s an hour later.

42 hours to go

Due to the competition starting at 1AM our time we figured we’d be wiser to start the competition by sleeping, so by now we’ve only been at it for roughly 3 hours. Progress so far: Breakfast has been eaten, and a basic application skeleton has been deployed to our server, running a very, very, very basic version of our core functionality.

We’ve used Bort (as I am sure a ton of other teams has as well) as the base for the application, and I am sure we’ve have saved some time doing just that.

Next time around I’ll probably take a look at the base application before actually basing an application on it, though. Not that there has been any issues, but we did waste some time figuring out some Bort details. Luckily, it’s time to dig into the meat of the application now.

Rails 2.0 deprecations

The Rails core team is cleaning up. That’s obviously a good thing, however it does leave us facing a kabillion deprecation warnings when running tests and whatnot.

Unfortunately the official deprecations page appears to have been thrown together quickly after someone noticed that Rails 1.2 had been pushed out without anyone finishing the page that all deprecation warnings were referring to.

So what does a smart Rails developer who wants to be ready for Rails 2.0 when that time comes around do? She reads on…

Simplifying my Rails views

Rails views are powerful creatures. You have the full power of Ruby right at your fingertips, which makes it easy to write too much Ruby code in your view making them become cluttered and hard to read.

The following is the tale of how I cleaned up a small part of a single view in BiQ after having made a dirty mess of it.

Phew, Rails Day is over

After 24 hours of insane, non-stop coding, designing, talking, and general tom-foolery, Rails Day 2006 is over. Nearly 200 teams competed, committing over 5000 total changesets to the official SVN server.

My team

Even though I got a team fairly late, and our initial design/concept wasn’t fully fleshed out, I reckon we got fairly far. The application was inspired by Dan Russells post on Creating Passionate Users about visualizing your life as dots.

The application got almost feature complete, although it got far from as usable as I would have liked it to. We’ll have it deployed to a webserver near you soon. Our team stats are here, for those who care.

Is Rails Day 2007 coming soon?

Rails Day was a really cool (and tough) experience, and I had a lot of fun. Thanks should go to my teammates and our “manager” (yes, Johannes, that’s you ;) ), and to the Rails Day crew. The spectate function, SVN bots, and generally everything has been running pretty smootly, cheers! Also huge thanks to my lovely girlfriend who took good care of me all day, and didn’t even once call me crazy or roll her eyes – at least not while I noticed.

Next year, I am hoping the danish Rails community will be crawling with people so we can field a team all sitting together. That’d be extremely cool in my book. So, see you next year?

Railsday is acoming

This coming Saturday will bring about 24 hours of Rails programming mayhem. Participating would be extremely cool and fun to boot, but I have no team or no really good ideas for a killer application.

So uhm, anyone need a Rails developer with a flair for UI design?

Update: That didn’t take long, looks like I’ll be competing in Rails day for the first time. Cool, looking forward to this.

Kill the Rails pluralization

One of the oft-ranted about features of Rails, is the Inflector – also known as that f***** pluralizer. To turn it off, put this in your config/environment.rb, below the line that says “Include your application configuration below”:

ActiveRecord::Base.pluralize_table_names = false

This works in Rails 1.0, and counters seemingly weird issues where Rails can’t find your tables (like the “Before updating scaffolding from new DB schema, try creating a table for your model” message from the scaffold generator).

To figure out what the pluralizer does and expects, do check out Geoffrey Grosenbach’s Pluralizer Tool.

How to stay on Rails 0.13.1

In these times where Rails 1.0 Release Candidates introduce memory leaks, bugs, and whatnot, you might want to lock your Rails application to a tried and tested version.

In your config/environment.rb, change your require_gem lines from

require 'active_support'
require 'active_record'
require 'action_controller'
require 'action_mailer'
require 'action_web_service'

to

require_gem 'activesupport', '= 1.1.1'
require_gem 'activerecord', '= 1.11.1'
require_gem 'actionpack', '= 1.9.1'
require_gem 'actionmailer', '= 1.0.1'
require_gem 'actionwebservice', '= 0.8.1'
require_gem 'rails', '= 0.13.1'

and you’ll be rolling on Rails 0.13.1 until you decide not to. Best way to make sure breakage doesn’t happen when your host decides to upgrade without warning you.

Respect my table name prefix

What’s wrong with this line of Ruby on Rails code?

Car.find(:all, :conditions => ['cars.year = ?', @year])

If you’re doing your own, internal application, nothing major is wrong. However if your application is meant for distribution and end-user-deployment, you’re making dangerous assumptions about the database by hardcoding the table name.

Readable Rails code

Man, I love how humanly readable Ruby on Rails code can be. I mean, I reckon even my mother could guess what this line of actual code does:

redirect_to login_url unless current_user.has_permission_to(MANAGE_USERS)