Archive for the '/dev/rails' Category

Don’t Preface Partials With Numbers

I just learned today that it’s a bad idea to name your Rails partials starting with a number, ie: _4_happy_places.html.erb. Turns out Rails throws the following error if you do:

`@4_happy_places’ is not allowed as an instance variable name

This has been tested against Rails 2.0.1.

No Comments »

chris on December 9th 2007 in /dev/rails

Google Map Markers in YM4R/GM

When using YM4R/GM, by Guilhem Vellut, in your Rails app chances are you’ll want to mark points on the map using custom icons. The means to do so is not readily apparent or straight-forward. I’ve found that this works well:

map = GMap.new( Constants::GM_DIV_ID, Constants::GM_NAME )

# Define the start and end icons
map.icon_global_init( GIcon.new( :image => "/images/google_maps/icong.png", :icon_size => GSize.new( 24,38 ), :icon_anchor => GPoint.new(12,38), :info_window_anchor => GPoint.new(9,2) ), "icon_start")
map.icon_global_init( GIcon.new( :image => "/images/google_maps/iconr.png", :icon_size => GSize.new( 24,38 ), :icon_anchor => GPoint.new(12,38), :info_window_anchor => GPoint.new(9,2) ), "icon_stop" )

icon_start = Variable.new("icon_start"); icon_stop = Variable.new("icon_stop")

if respond_to?( "start_lat" ) && respond_to?( "start_long" )
    map.center_zoom_init( [start_lat, start_long], Constants::GM_ZOOM )
    map.overlay_init( GMarker.new( [start_lat, start_long], { :icon => icon_start, :title => name + ” start”, :info_window => “start” } ) )
end

if respond_to?( “end_lat” ) && respond_to?( “end_long” )
   map.overlay_init( GMarker.new( [end_lat, end_long], { :icon => icon_stop, :title => name + ” end”, :info_window => “end” } ) )
end

1 Comment »

chris on November 28th 2007 in /dev/rails

Of Sivers, CDBaby, Rails and PHP

A couple days ago Derek Sivers of CDBaby (disclosure: I’m a big fan of the site, though perhaps not the current design) wrote an O’Reilly article titled ‘7 reasons I switched back to PHP after 2 years on Rails’ about how he spent two years trying to rebuild his site in Ruby on Rails before switching back to his tried-and-true PHP to finish the job. If you’re a developer give it a read, it’s well worth it. Unfortunately the article has also stirred up a hornet’s nest of comments and criticisms, many from respectable blogerati and quite a few in response to the comments received both on the O’Reilly article and on the Slashdot post.

To preserve my sanity and faith in humanity I don’t read Slashdot comments - those I leave for fools and masochists - but I did decide to go back and re-read the original article and then the subsequent comments. I gotta say: I’m really disappointed in the tech community in general. You’d think someone just walked up to them and said “Damn, your kid is stupid and ugly, and that’s his good side” by the way we’ve reacted. I just don’t get it.

After re-reading the article I was struck with a number of thoughts that just wouldn’t leave, first and foremost: why wasn’t Rails suitable? As a Rails developer when a really smart, successful developer comes along, spends two years trying to work with my (currently) favourite framework and finds a problem with it, I sure as hell want to know what those problems were because chances are they found limitations and edge cases I haven’t even begun to explore yet and some day they might bite me on the ass. I want this guy to write more articles about his project; about the Rails aspect, the PHP aspect, the failures, the successes. How many other projects of this magnitude can claim to have been developed in two competitive platforms like this?

Caveat: I’ve been a PHP developer for years before I was a Rails developer and I really like PHP, warts and all. I really like Ruby too, but I don’t yet profess to know it nearly as well as I do PHP. I’ve only built three production sites in Rails so far so my experience is still limited.

I also wondered: Rails of two years ago (before my time) strikes me as an perhaps an immature platform from what I know of it, primarily usable only by those intimately familiar with it and for a specific types of applications. Derek clearly knew this because he hired Jeremy Kemper so how does the Rails of yore stack up against the Rails of today? Does Jeremy still think the barriers would be there?

I wondered: how did Derek reduce the PHP codebase by so much? It’s not magic, he clearly knows what he’s doing and understood his system to an impressive level (he reduced by a factor of 8). I want technical details about his optimizations.

I wondered: where does a guy find the balls to scrap two years of work and start all over from scratch? As the client he clearly had the authority to do so but man, how many of us can say we’d step up and do that instead of continuing to try and shoe-horn our “solution” just a little bit longer. There’s some lessons in the thought processes and decisions that went into that choice, I’d like to hear them.

Even a second time through I was having fun with this article.

But then I got to the comments. So much venom levied against a guy who basically said “Huh, a Torx wrench doesn’t work as well as my trusty Phillips screwdriver for this project, guess I’ll use the Phillips.” Are we developers really so fragile that we can’t stand the thought of someone doing something that isn’t our way? Are we really so arrogant as to think our experience the only one worth having?

Comments like this are just pathetic, they shame us all:

Useless post without a concrete illustration.

Looks like you among many out there had a poor team. Poor development environment where you were working on several projects at once and putting priorities in wrong order among several projects. I will have to say being stuck in one mental framework can be hard to overcome, especially when learning to code in a new and cleaner manner as per Ruby on Rails. But you actually could have coded so much more faster had you stuck to your guns and not given up, or not, since you prioritized among so many projects.

Why didn’t you hire a Rails core member? Who is Jeremy Kemper? I have been programming in Ruby for 4 years now and I have never heard of him.

(I wanted to not comment on the comments but this one take the cake. Not only did this poster obviously not read the article - the answer is only in the second paragraph - his gross arrogance and incredible lack of knoweldge and Google-fu would immediately make him a “never hire” for me. Aaron, Google up a clue.)

Reading your reason #6, I see that you totally don’t get what rails is. And thats pretty sad as a resumee after 2 years.

Oh and I just checked out your site. What an ugly P.O.S! 2 years and you can’t build craigslist for CD’s using rails. Jesus.

Etc. etc. etc.

We’re developers. We build things. We create from nothing the beautiful things that the rest of the world uses, uses without ever thinking about how those things were built. Only we care about the creation process but I think in this case we’ve lost sight of where we developers end and our tools and processes begin. We are not our tools, we’re the sum of our output. To become emotionally attached to a tool or methodology is to limit ourselves in a terrible way. They’re just lines of code, collections of bytes, arrangements of magnetic dust on metal platters. Geez.

This guy scrapped his original site to rebuild it from scratch on a hunch it might go better. When that failed he didn’t get religion and scream and cry, rend his hair, curse the gods. He scrapped it and found a better way. And then he built it.

That, ladies and gentlemen, is the essence of a real developer.

Comments Off

chris on September 25th 2007 in /dev/rails, /dev/random

Reloading dispatch.fcgi

Rails development is usually done using dispatch.cgi so changes are readily apparent. However dispatch.cgi is painfully slow for a production environment so it only makes sense to use dispatch.fcgi. You can check which you’re using by looking into your /public/.htaccess file. In this case I’m running dispatch.cgi and .fcgi is commented out:

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
#RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Problem is dispatch.fcgi won’t immediately recognize changes made to content. In order to force a flush of all dispatch.fcgi instances we could kill all the processes but this seems better to me:

[chris@enki ticklist]$ touch public/dispatch.fcgi

No Comments »

chris on September 11th 2007 in /dev/rails

Dr. Nic Sells His Baby

Well, one of them anyone and maybe not the one he loves the most.

All-around pretty smart Rails-guru guy Dr. Nic is selling his Rails-based MyConfPlan site for charity so he can attend a conference session:

Why? Because all the money paid by the winner is going to the Chad Fowler and Marcel Molina Jr fund for all-things Humanitarian, and so I can go to their Testing Training Day on the 17th of September in Berlin. Minimum donation is $75.

That’s right, the winner’s money could be entirely tax-deductible as its all going to charity. (You get the site’s code base and the myconfplan.com domain (cost of transfer is yours) are yours. I get entry to the workshop.

If it raises $500 then you are donating $500. If it raises $2,000,000 then you are donating $2×10^6.

I like it. Rather than let an interesting niche site languish on the vine it gets sold off to someone who can either learn from it or extend it, Dr. Nic gets some testing edumacation, and the charity gets some cash.

As of this posting bidding is up to AU $355. Not too shabby.

MyConfPlan in action.

No Comments »

chris on August 23rd 2007 in /dev/rails

Handy bash Aliases

Today I finally got around to editing my .bash_profile to include the following aliases. I find they make general command line use more pleasant.

The first one remaps ls to show a useful amount of information. Never in my lifetime of using computers have I ever wanted ls to output the way it does by default.

The next two just save a couple of characters by removing the cd from their respective command and optimizing the dual backstep (a cool shell script would be one that counts the number of consecutive periods and cd's back that many directories - 1).

The last one is Rails-specific and simply fires up the default server from your Rails project directory.

alias ls=’ls -alh’
alias ..=’cd ..’
alias …=’cd ../..’
alias home=’cd ~’

alias ss=’script/server’

Update: See Sam’s suggestions for fast ls variants below.

3 Comments »

chris on August 16th 2007 in /dev/rails, /dev/random

Rails ActiveRecord Instances and ’super’

For a class of objects in my database I extract data from one or two ways: the first directly from the database via ActiveRecord, the second from Amazon using Amazon::Ecs.

When the asset calls to Amazon to populate its properties I want to use the Amazon-provided data. When I refrence the asset without calling to Amazon (ie: the data has been cached) then I want to reference AR's properties directly. I don't want to have to make two calls dependent upon the data's source location.

In otherwords, I always want to be able to call

book.title

no matter what.

Turns out ActiveRecord makes this pretty easy:

RUBY:
  1. def title
  2.   return @atts ? @atts.get( 'title' ) || "" : super
  3. end

If there are attributes and an attribute called "title" exists, return that. If attributes don't exist then call the super class's title() which contains the data extracted from the database.

Pretty cool ActiveRecord, pretty cool.

No Comments »

chris on August 8th 2007 in /dev/rails

Where Does “– tables()” Come From?

When starting up one of my Rails apps the initial console output looks like this:

=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
-- tables()
-> 0.0576s
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).
** Rails signals registered. HUP => reload (without restart). It might not work well.
** Mongrel available at 0.0.0.0:3000
** Use CTRL-C to stop.

Note that little chunk in the middle that shows how long it took to read the table structure from the database:

-- tables()
-> 0.0576s

Where the heck does that come from??

Of the three Rails apps I currently have in development that only gets output in one of them. The config and environment files are the same across all three platforms (save for database access credentials) and yet only one of them spits out that data.

It's really messing up the production server since Apache attempts to jam it into the HTTP header which causes a 500 error, as one might expect.

Anyone know? A plugin? Lesser-known config option? Elves? I am stymied.

No Comments »

chris on July 19th 2007 in /dev/rails

Solving the Depricated @Flash Warning

Long long ago I ran into an issue in which my use of flash always generated a warning, though I thought I was implementing it properly.

In the comments Dmitry finally solved the mystery:

Don’t name your flash partial “_flash.rhtml”, name it something else, like “_flash_box.rhtml”. This is a known bug.

And with that, no more warnings.

No Comments »

chris on July 10th 2007 in /dev/rails

The Power of rake

rake is one of those little things about Ruby on Rails that I suspect most of take completely for granted, running the odd rake tast without really ever thinking much about it. Turns out rake is pretty damned cool.

Gregg over on Rails Envy has written a very nice intro to rake in Ruby on Rails Rake Tutorial (aka. How rake turned me into an alchoholic) :

In this article we're going to discover why Rake was created, and how it can help our Rails applications. By the end you should be able to write your own tasks, and learn how to get piss drunk using rake in no less then three steps.

He's written well enough that by about midway down the article I was already thinking of ways to use rake to do some of the unpleasant maintenance tasks every site needs that I was consequently putting off thinking about. rake: not bad at all.

No Comments »

chris on June 12th 2007 in /dev/rails, /dev/ruby