About concept47

Ruby/Rails/Jquery/MySql/PostgresSQL hacker. Wannapreneur. Love to build beautiful things.

How to get a 25% boost in your Rails app?

I was working on a rails app today, and got to wondering about the very many

SHOW FIELDS FROM

MySql queries I was seeing.

I had always thought about whether there was a way to get rid of those completely, or reducing the number of them by caching or something.
Well, today I actually did some digging and found this little gold nugget about “SHOW FIELDS” calls in Rails.

Apparently, Rails does cache MySql “SHOW FIELDS ” with one notable exception.

However, upon inspection it turns out that Rails DOES NOT cache ‘SHOW FIELDS’ queries for has_and_belongs_to_many associations. So every time a select or an insert is done via a Rails has_and_belongs_to_many association, a ‘SHOW FIELDS’ on the join table is executed.

So the dreaded HABTM association strikes again!
Those darned things are pure evil eh? 😀

Well the awesome blog writer figured out a patch to fix the problem.

And in his tests he reported this (see image below for reference) …

After rolling this change out we have seen a very noticeable improvement on performance. By using New Relic’s Compare With Yesterday feature (see chart below) we can see that our application response time dropped from an average of about 560 ms per request to 420 ms per request. This is roughly a 25% performance increase. The yellow line shows today, the blue line is yesterday. CPU and database load decreased by about 25% as well.

A 25% improvement in application response time!?!?!
Sign. me. up.

New Relic monitor app showing 25% improvement in response time

I followed the Rails ticket on this particular issue and couldn’t figure out whether the fix had been rolled into a Rails version yet. I asked the blog author and he says the patch has been included in Rails 2.3, so if you’re running that, this does not apply to you.

I’m about to apply this patch to a couple of applications, hopefully it helps you too.
Please post in the comments about your experience. Same? better? worse?

R.I.P Michael Jackson

Yes I know this is supposed to be a professional blog.
But today, a man I considered the greatest ever musician in modern history … died.

Thank you for the awesome memories Mike.
You inspired me more than you’ll ever know.
You were amazing.
I’ll miss you.

20090626-mj-dead-at-50

Steal my idea: dual videocards/video outputs on a laptop

Why doesn’t someone make a laptop with dual video cards on it?
That way you can run two displays in addition to your laptop instead of one.

It wouldn’t have to be able to run a 2560 x 1600 display on both cards, 1920 x 1080 will do (that’s two nice 24″ displays). I know lots of developers who’d pay good money for that.

PS: As with all ideas posted on this blog, feel free to take them and use as your own.
Whether you’re successful or not, it’d be awesome if you’d write back and let me know 😀

Netbeans 6.7 rc2/rc3 not ready for primetime?

Regarding my post yesterday about Netbean’s new 6.7 rc releases

While my initial user experience was dandy, I have since discovered some show stopping bugs.

– When trying to import your plugins from a previous version (6.5 in my case), the screen that pop up mysteriously disables mouse clicks.
So you basically get stuck at a screen like this one …

Netbeans plugin export fail

… with no possible way of moving forward or back.
I had to kill the netbeans process and restart (happened in both rc2 and rc3).
To be fair, after restarting, I had no more problems … the plugins seemed to have imported just fine.

– I also discovered another odd problem while trying to work on migration files in Rails. After Netbeans produced one intellisense dropdown, the entire IDE would start freezing on every 2 or three key strokes … making itself unusable. I had this problem in rc2, no such problem with rc3.

– On the plus side, startup time is *a lot* faster than in 6.5. As soon as I loaded a few modules the startup speed went back to be cell-phone-customer-service slow, so no plus there.

+ The whole netbeans process seems to be self contained now. It used to be that when you looked in process explorer, you’d see Netbeans, nbexec and java, with the real size of netbeans contained in the Java process.
This, happily, is no longer the case. Netbeans still easily tops 200MB of memory though :\

– Trying to create a new rails app, the browse button on this screen does not work, terrible eh?

browse button on 'create new project' doesn't work in Netbeans 6.7

Fixing Ruby irb window console in Netbeans for Windows

PS: This worked for Netbeans 6.7 RC2, however it will HORRIBLY break any attempt to use irb from the windows command line

If you right click on a project in Netbeans then go to ‘Ruby Shell (IRB)’, you should get dropped into a ruby IRB window.

Unfortunately for most of you on windows, you might get this instead …

Netbeans irb window fails on windows

An easy fix is to go to irb.bat and comment out the first 7 lines of code so that it looks like this

#@echo off
#@if not "%~d0" == "~d0" goto WinNT
#\bin\ruby -x "/bin/irb.bat" %1 %2 %3 %4 %5 %6 %7 %8 %9
#@goto endofruby
#:WinNT
#"%~dp0ruby" -x "%~f0" %*
#@goto endofruby
#!/bin/ruby
#
#   irb.rb - intaractive ruby
#   	$Release Version: 0.9.5 $
#   	$Revision: 11708 $
#   	$Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
#   	by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
 
require "irb"
 
if __FILE__ == $0
  IRB.start(__FILE__)
else
  # check -e option
  if /^-e$/ =~ $0
    IRB.start(__FILE__)
  else
    IRB.setup(__FILE__)
  end
end
__END__
:endofruby

Netbeans 6.7 Release candidate/RC 3 released

Netbeans 6.7

I’d been using 6.5 for the last couple of months, forgetting to look for updates in my RSS feed, when I came across the announcement of Netbeans 6.7 RC 2.

I quickly downloaded it and started using it, when I found out that RC3 was released just yesterday as well, I’ve downloaded it, but since I’m in the middle of a slew of projects I won’t be installing it until the weekend.

However with RC2, I am impressed that they finally fixed the silly problem of each version of Netbeans not importing settings from previous versions.

The install went smoothly and I was up and running faster than usual.

The things that jumped out at me are

+ There is no annoying subversion connection window that jumps up in the output section for each project that you’re working on any more

– The Ruby irb output window still doesn’t work for me (see screenshot)

Netbeans irb window fails on windows

– Still no word wrap (I know they’ve said to expect it in version 7.0, but seriously …)

– I spent almost 2 hours yesterday trying to figure out what changed between RC3 and RC2, and I couldn’t do it.
If you can, please drop me a line.
Apparently with each new release, the documentation page updates to the current release. So basically there isn’t a release page for RC2 any longer (please fix this guys).

+ For Ruby on Rails here are the things that are new (nothing exciting really)
Here are the release notes for RC3

Ruby and Rails

  • Remote debugging support
  • Improvements to Ruby constants support
  • Run and debug actions for test cases and suites included in context menu
  • Support for Shoulda tests

enjoy.

requiring gems from github (specifically flickr-fu and xml_magic) using config.gem

So the rails way of requiring a gem in your app is by using the config.gem instruction in environment.rb (as opposed to sticking a require statement in your environment.rb).

However including gems from github is a different beast … sometimes the gem author will tell you exactly how to do it, or sometimes they won’t, as the case with flickr-fu.

Just fyi … typically when you are including a gem from git hub you will want to do something like this …

config.gem 'gem-name-from-'gem -list'-command', :lib => 'github_gem_name', :source => 'http://gems.github.com'

“gem name from gem -list command” … by this I mean that you should run a gem -list command from your command line and use the name that shows up for your gem there.

… so the will_paginate gem config.gem statement looks like this …

config.gem 'mislav-will_paginate', :lib => 'will_paginate', :source => 'http://gems.github.com'

but for the xml-magic gem, its this ….

config.gem 'xml-magic', :lib => 'xml_magic', :source => 'http://gems.github.com'

and for the flickr-fu gem, its this ….

config.gem 'flickr-fu', :lib => 'flickr_fu', :source => 'http://gems.github.com'

Its not documented anywhere.

I had to figure it out by trial-and error, now you don’t have to 😀

Southwest Airlines error screen fail

cropper_capture_04-13-200901-58-29-am

Looks alright doesn’t it?

Problem is, there are *absolutely* no clickable links on that error message.

Go ahead … try it.

How to fix it?
You need a link back to the home page at least, suggesting other things the user might want to check out is not a bad idea either.
And for style points, a simple text box to get information about what caused the error to happen, will alert you to the problem so you don’t lose prospective customers.

How to uninstall a plugin in Rails

This one is simple, but I couldn’t find a decent google result for it.

Before you uninstall the plugin, you have to get its name … go to the vendors directory in your app folder and get the name of the folder

In this case the name of the plugin is “active_scaffold”

Go to the directory of the app you want the plugin removed from and type in

ruby script/plugin remove active_scaffold

or if you’re on a Linux box

./script/plugin remove active scaffold

Cakephp Filter gotcha for rails folks

Usually in Rails, if you specify a before_filter in the base controller ‘ApplicationController’ (in application.rb), every other controller in that app inherits that filter, so that even if you specify a before_filter in another controller … the filter in application.rb always runs
Example:

class ApplicationController < ActionController::Base
   before_filter :check_login
end
class UploadsController < ApplicationController
    before_filter :get_data
end

The :check_login method is always run even though UploadsController specifies another before_filter.
(You can stop this behavior by specifying a skip_before_filter :check_login in the Uploads Controller)

However if you take this mindset with you to cakephp Continue reading