Truly Awful Rails Code (Send it to me!)
There's tons of material out there telling us how to properly write code (or attempting to anyway), but in an effort to be creatively different, I decided that my Railsconf talk this year will be all about bad Rails code. Entitled "The Worst Rails Code You've Ever Seen...", I'm basing it on both code and practices that I've both done myself, stumbled across as a neutral observer or rescued during the last few years.
After working through a draft, I'm feeling a little short on examples, so I'm reaching out to you for some help. Please email me your heinous Rails coding examples, stuff that you're truly embarrassed about. I'm looking for stuff that has obvious "better ways" or not, the latter being good topics for discussions of how Rails occasionally leads developers down the wrong path.
If you want to be a little bit more anonymous than email permits, you can also post a comment to this entry linking to snippets on Pastie. Not to worry, none of the example code in the talk will be direct copy-paste of actual client code or stuff that you submit. I plan on changing variable names and anonymizing stuff to the best of my ability and for my legal protection.
If I get enough material, I plan to make the content of the talk and submissions into a regular series of posts on this blog. Thanks in advance for your help!




I want to be clear I didn't write these, and when I saw them I had to stare at them for awhile and let it perculate in my brain like when you fill your mouth with coca cola and pop rocks:
Example1
http://pastie.caboo.se/199740
Example 2
http://pastie.caboo.se/199742
Posted by:Tim Case | May 19, 2008 at 04:07 PM
For the last year we've documented a bunch of atrocities at blogyourshame.com.
Posted by:sandofsky | May 19, 2008 at 04:46 PM
Nested interpolations:
http://pastie.caboo.se/199829
Just because sometimes we're too lazy to write a helper method that will only be used once, or to write a 12-line conditional tree... only to realize a couple of months later that we've copy/pasted that same expression a dozen times.
Posted by:Sebastian | May 19, 2008 at 05:33 PM
Had to write the following code yesterday evening:
http://pastie.caboo.se/200074
Because a simple "update_attributes" or "attributes=" followed by "save(false)" wouldn't work (ignoring the newly added columns) when migrating down several migrations.
"update_attributes" works nicely when migrating down only this single migration...
There are things I don't understand sometimes :P
Maybe someone has got an answer :)
Posted by:Damien | May 20, 2008 at 03:49 AM
Well it seems ActiveRecord::Base#reset_column_information is the answer to the awful code I've written :D
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001408
Posted by:Damien | May 20, 2008 at 06:24 AM
I guess this is how most of us start coming to rails from other technology..
http://pastie.caboo.se/200140
Posted by:RD | May 20, 2008 at 07:38 AM
This is some of the worst code I've ever seen. It's an action on a controller.
Take note of the global variables. I had to ask around on the atlrug irc channel because I'd never seen a global variable in Ruby before. I figured the dollar signs were just from a former PHP developer.
Can you imagine whoever wrote this protesting that "it works fine on my local machine..."?
Ugh...
http://pastie.org/200200
Posted by:Brandon Beacher | May 20, 2008 at 10:10 AM
I found one this morning in ActiveResource::Base#method_missing:
case method_name.last
when "="
attributes[method_name.first(-1)] = arguments.first
...uh...
first(-1) ?!?!? What's that supposed to do?
And looking into ActiveSupport::CoreExtensions::String::Access the
plot thickens:
def first(limit = 1)
chars[0..(limit - 1)].to_s
end
My favorite aspect of the whole thing might be that -1 becomes -2 along the way.
Posted by:Ryan Platte | May 20, 2008 at 11:08 AM
for the safety of you and those you love, please wear the appropriate goggles when viewing this code.
http://pastie.caboo.se/200240
Posted by:colin harris | May 20, 2008 at 11:32 AM
I'm pretty sure this is going to take the cake. It's already been featured in a RailsWTF?
Apparently, the developer(s) who wrote this decided that filters just weren't hip enough. Instead, they pushed all responsibility for permissions into one method in the super class, the Application Controller.
It's so bad that it's awesome in its own way. :)
http://pastie.caboo.se/200406
Posted by:jaw | May 20, 2008 at 03:43 PM
This is code from a project that just got dumped on me. Not sure what part of "validation" this developer didn't understand...
http://pastie.caboo.se/201820
Posted by:Jason Cartwright | May 22, 2008 at 04:57 PM
No offense, but most of these just seem to exemplify newness to Rails or Ruby itself (basic ignorance of included functionality that would save tons of hassle); in spite of that, they mostly exhibit approaches that seem reasonable for developers coming from other languages and frameworks, and don't look especially error-prone, just overly verbose and fragile in the long run. None of them hold a candle to many of the atrocities on display at thedailywtf.
Come on people, is this really the worst we can do? Are we going to let Rails be bested by the likes of VBScript in a race to the bottom?
Posted by:Jack Nutting | May 23, 2008 at 05:33 AM
This one transcends the newbies on rails theme. The guy who wrote this clearly knew a thing or two about ruby. Keep in mind there's not a single word of documentation on this snippet.
http://pastie.caboo.se/202399
Posted by:Aaron | May 23, 2008 at 03:11 PM
Hope this qualifies:
http://pastie.caboo.se/205497
it's commented as "the single ugliest Ruby line I've ever written".
Posted by:Roadmaster | May 29, 2008 at 01:21 PM
jaw,
Is that code from a certain rails based social networking site by any chance? It looks awfully similar to some equally horrible code I've seen recently.
Posted by:Glenn Gillen | June 03, 2008 at 07:43 AM
Glenn,
In a word, yes. Not the same app, but it's from the same people.
It's sucking the life out of me.
Posted by:jaw | June 03, 2008 at 06:40 PM