Tuesday, 25 September 2007

MySQL Temporary Tables and Rails

Note: The RichText blog has moved to www.ricroberts.com

According to the Rails documentation, all active record classes by default use the same database connection.

As the Agile Web Development With Rails book rightly says, Ruby on rails is not thread safe (2nd Edtn p 649). A Mongrel server uses one thread per request (i.e. multithreaded), but is synchronised around the calls to Dispatcher.dispatch. This means that everything is threaded right before and right after Rails runs. While Rails is running there is only one controller in operation at a time. This is why people typically have to run a small set of Mongrel processes (a “Pack of Mongrels”) to get good concurrency (from Mongrel FAQs).

MySQL temporary tables are only visible inside the connection from which they are created, and they are automatically dropped when the connection is dropped (see the MySQL docs). Along with the above, this means that any rails active record class will be able to see temporary tables created by other active record classes (within the same server process).

However, since only one controller operation is running at a time within each server process (and therefore only one for each connection), I think you can safely do the following in a rails active record model class if you want to use temporary tables:

begin
connection.execute("DROP TEMPORARY TABLE IF EXISTS my_temp_table")
connection.execute("CREATE TEMPORARY TABLE my_temp_table")
# do stuff with temp table here
ensure
# this drop is here to help keep the size of the data base down
# between calls to this method
connection.execute("DROP TEMPORARY TABLE IF EXISTS my_temp_table")
end



Digg Technorati del.icio.us Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

Please also visit the Swirrl blog

Monday, 10 September 2007

NetBeans Intervenes

Note: The RichText blog has moved to www.ricroberts.com

Regular readers of this blog will know that I'm not averse to trying new IDEs for my rails development.

I recently came across this post by George Cook about an app I hadn't considered.... NetBeans.

George does a great job of explaining NetBeans's virtues, so I won't repeat them all here, but to sum up, it does everything I need a rails IDE to do plus a little bit extra. The most impressive feature for me is the in-IDE fast debugging (more on how to set this up below).

NetBeans does have it's down points. With its default settings it doesn't look as slick as TextMate, but after some fiddling around with the preferences you can rectify this. It does use more memory than TextMate but even on my MacBook with 1GB RAM and lots of other applications open, I still have a little bit free. Besides, compared with something like Microsoft Visual Studio 2005 (which eats RAM for breakfast) NetBeans is a veritable size zero.

There is a ruby-only build of NetBeans available which helps to keep the memory footprint down. The exact version I installed was build 3533, which seems to work fine.

UPDATE (26 Sep 07): Netbeans 6 Beta 1 is now out! Get it from the Sun web-site.

Getting started with NetBeans

Here are few quick pointers to get you going.

To begin, all you have to do is do:
-> File -> New Project
.. and then choose "Ruby on Rails Application with Existing Sources"
and browse for your rails folder.

You need the ruby-debug-ide gem installed (sudo gem install ruby-
debug-ide) to do fast debugging.

Before trying to debug, you need to go to:
-> Preferences -> Ruby -> Platform
... then choose the location of your ruby interpreter (e.g. /usr/local/bin/ruby)
You should then be able to select the Fast Debugger radio button for the
Debugger engine. (If you can't, try restarting NetBeans - this worked for me).



Digg Technorati del.icio.us Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

Please also visit the Swirrl blog

Wednesday, 5 September 2007

RedBox Rules!

Note: The RichText blog has moved to www.ricroberts.com

As you might have noticed from previous posts in this blog, I'm somewhat interested in modal dialogs in Ruby On Rails.

Up until now, I've been happily using the Prototype Window Class. However, it's quite a lot of js (a lot of which I don't use), and I've recently found a few small bugs in it. I'm not denying it's an impressive piece of work - it's just that there were a few niggles (Check out the forums).

Anyway, I've been investigating a few other options (namely: SubModal, ThickBox, LivePipe's Control.Modal, LightBox Gone Wild, StickyWin) and then I eventually found RedBox.

Most of the options I considered are variations on the LightBox theme, but RedBox stood out for me because of it's simplicity and ease of use with RoR. It doesn't do anything particularly fancy, but it ticked all my boxes (i.e. easily customisable, works well with Rails, small size of javascript, no obvious bugs).

It really couldn't be simpler to use RedBox with Rails.

To install the RedBox plugin, in Terminal just run:

script/plugin install svn://rubyforge.org/var/svn/ambroseplugins/redbox


...from your Rails project folder. This will stick stuff in your vendor folder. Then, from the vendor/plugins/redbox folder, you can run:

rake update_scripts


...to copy the files to the relevant places in your project. (i.e. the public/javascripts, stylesheets folders etc.).

Now all that's left is to stick the javascript and stylesheet links into your layout:

<%= stylesheet_link_tag 'redbox' %>
<%# we need to include prototype, etc... %>
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'redbox' %>


To create a link to a RedBox, you can use the helpers that the author provides, the most useful of which I found to be the 'link_to_remote_redbox' helper. This allows you to launch a RedBox with some content from another page. For example:

<%= link_to_remote_redbox("redbox",
:url => {:controller=>'my_controller', :action=>'my_action',
:id=>'my_id'}) %>


Then, in the resulting RedBox dialog, you need some way of closing the it. To just close is very simple...

<%= link_to_close_redbox "Cancel" %>


It's also pretty easy to get it to do stuff after closing. For example, you could call a rails action, via an ajax call...

<%= link_to_close_redbox "OK", {:onclick => remote_function(
:url => {:controller=>'my_controller', :action=>'my_action',
:id=>'my_id'},
:complete => "$('spinner').hide();",
:before => "$('spinner').show();")
} %>


If you want, the rails action could do some rjs to update some of the main page.
(Of course, you could use a similar technique to submit a form on the dialog too).

One of my additional requirements was to be able to guide the users through a two-step process, within the constrains of the modal dialog (something I found fiddly with other modal dialog solutions). Here's how I did this with RedBox...

<%# to link to another page inside RedBox %>
<%= link_to_remote( 'hello',
:url => url_for(
:controller => "my_controller",
:action => "my_action",
:id => "my_id"),
:update => 'modal_content', # the div used by default for the dialog
:complete => 'RedBox.setWindowPosition();'#make sure it's centered
) %>


Notice how I call the RedBox's setWindowPosition() function once the update is complete. This is because the new page being shown might be a different size to the first one, and although the dialog automatically resizes to fit the content (which is nice), it doesn't automatically recenter itself. (Without this step, the dialog's top-left corner just stays in the same position).



Digg Technorati del.icio.us Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

Please also visit the Swirrl blog

Tuesday, 4 September 2007

A new look

Note: The RichText blog has moved to www.ricroberts.com

I've decided to give my blog a new look, as I've been neglecting it recently.

I've got a few tasty computing morsels up my sleeve, so watch this space! ;)



Digg Technorati del.icio.us Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

Please also visit the Swirrl blog