Satya's blog - 2008/11/

Nov 24 2008 10:25 Rails: Override Table Name Quoting

We had a situation where our Sybase database didn't appreciate that Ruby on Rails was quoting table names like so: [table_name]. The square brackets are okay in Sybase but our particular database was too old for it. So, I have to override table name quoting. In my Rails app, in config/initalizers/sybase_adapter.rb, I place this code:

# sybase_adapter.rb
# stolen from rails' to override the table name quoting. []-quoting throws off atlas server.

module ActiveRecord
  module ConnectionAdapters
    class SybaseAdapter < AbstractAdapter # :nodoc:
      def quote_column_name(name)
        # If column name is close to max length, skip the quotes, since they
        # seem to count as part of the length.
        #((name.to_s.length + 2) <= table_alias_length) ? "[#{name}]" : name.to_s

    end # class SybaseAdapter
  end # module ConnectionAdapters
end # module ActiveRecord

And it works. Table and column names are no longer quoted. Much thanks to folks on the #rubyonrails freenode IRC channel. Specific users: %w[ blj Bobnation defswork jammanbo rsl ].join(', ').

Update: For some reason, this didn't work with active scaffold on Rails 2.1. Shrug.

Last updated: Dec 04 2008 13:43

Tag: geeky rails

Nov 20 2008 20:27 India declares War on Piracy

A BBC article about piracy in the Gulf of Aden says one-sixth of the world's sailors are Indian. This makes Indian sailors a big target for pirates. A lot of Indian shipping goes through the Gulf of Aden, apparently.

Cool. Real pirates suck.

Update: The "Cool" part was that the Indian Navy is going up against the pirates. At least one ship is patrolling the area, according to the BBC article

Last updated: Nov 21 2008 13:22

Tag: news

Nov 19 2008 08:02 PHP and Ruby on Rails compared

I received an email asking about differences between PHP and Rails. This is my reply. It has been cleaned up a little. It is not comprehensive.

Both PHP and Rails address the same domain: making dynamic web sites, in different ways. PHP, at heart,is a language that embeds itself in your HTML document. Now, it's OOP-ness and other things (such as template libraries) make it much more than that to the point that people forget what it is, but still. In Rails, the HTML page generation happens at the end of the request-response cycle....

Both are pretty newb friendly. I believe it's easier to write bad programs in PHP than in Ruby, though.

At this point I'd like to point out that Ruby is a programming language (notice I didn't say "web") and Rails is a web programming framework (notice I *did* say "web"). I point this out because people at work are always confused. And in the above paragraph, I compare PHP with *Ruby*, not Rails. When you build a RoR web site, a lot of the programming you do is in Ruby, hence the comparison.

As for function and speed: Rails, being Web-oriented in a Web 2.0 world (that means AJAX and dynamic HTML tricks are the norm rather than an exception -- and they're not to be used gratuitously, but only where needed -- and since many browsers are in Web 1.5 (I make that up) or earlier, AJAX should not be a necessity, which means it should be added on, not be the primary way to do things. As a general guideline, which I freely violate in controlled conditions. Professional programmer, closed subnet. Do not attempt.)

Ahem. So Rails has full and easy AJAX support, which means AJAX-y things are easy to do. You might be able to find code libraries in PHP, I don't know. Rails makes it really easy, though. It uses the MVC (Model View Controller) paradigm, and the View part is usually HTML, but it could be Javascript or XML, too. That's where you get half the AJAX functionalitiy.

(Not in the original email:) Rails includes the prototype and scriptaculous javascript libraries, which make AJAX and DHTML tricks very easy.

You may not need to do much AJAX anyway. I see your question stems from trying to speed up a potentially slow Rails app. Where did you hear that Rails is slower than PHP? It could be, but let's examine the bottleneck areas and comparison points:

PHP usually runs as a module of your web server (Apache). That makes the PHP interpreter pretty fast. Rails usually runs off mongrel or something else, which Apache has to proxy to. Apache is a decent web server, having it in front of your Rails server is a good idea. You can remove this bottleneck by running multiple mongrel processes for your Rails app, and have Apache do a load-balancing proxy. This is quite easy to do. Apache config:

    <Proxy balancer://yourapp_cluster>
        Order Deny,Allow
        Allow from all
    <Location /yourapp>
        RewriteEngine On
        RewriteRule ^/(.*)$ balancer://yourapp_cluster%{REQUEST_URI} [P,QSA]

(Mongrel listens on the ports in the BalancerMember lines.)

Now, mongrel does take memory, being a separate process. It's not so bad, though. You don't need huge resources, but I wouldn't run a Rails app, or much more than 1 or 2, off a virtual machine.

Speed of actual code: I can't compare. Of course, if you write an O(N^2) sort algorithm, you're screwed either way. (Hint: both PHP and Ruby provide sort functions, but this is only one example).

Database connection: This is likely to be the worst issue. Rails presents your database (DB) in an object-oriented manner, called Object-Relation Mapping (ORM). An ORM is usually pretty chatty with the DB, always asking for table descriptions and so forth. A lot of that can be optimized away, if you're reasonably careful about indexes, eager loading, and how you do your algorithms. Judicious use of periodic database maintenance scripts can help, too, by generating cache tables.

You're trying to optimize prematurely, as you've guessed. I believe Rails can support a pretty busy PBBG, depending on how busy each page is going to be.

Rails is used best on a server where you have shell access. You can set up a development environment on your Windows box, but you'll need a database. MySQL is recommended. For actual use, i.e. production, I recommend a Linux server, Debian preferred, where you have root access. Linode and csoft can give you virtual machines. Despite my earlier injunction against one, initially you can use a VM. If it gets busy, you can switch to dedicated hosting -- if you can make money off it.

Rails development happens with it's built-in web server, Webrick. Once you start it, (./script/server) you connect to http://localhost:3000/ . That's localhost port 3000. Do NOT use that for production.

Oh, if you want to try Linux for a home/development machine, grab an Ubuntu LiveCD:

Last updated: Nov 20 2008 20:36

Tag: geeky rails

Nov 16 2008 13:50 Text on web sites

Jakob Nielsen says "In real life, users often suffer under tiny text and websites that add insult to injury by not letting users resize the words."

That leads me to an annoyance: some corporate web sites have a "text resize" link, usually accompanied by a "+/-" or "Bigger font/smaller font" links. These are usually Javascript triggers to resize the text. The annoying thing is, that's not the web site's job!! That's your web browser's job, to resize text to your liking. Most web browsers have this function.

The annoyance becomes a pain when a "customer" requests the same function on a web site I make, because "BigCompany does it why can't you." It's not that I can't. I *won't*. You hired a systems analyst, not a code monkey.

Tag: geeky rant

Nov 11 2008 20:09 Windows XP re-install

Today I re-installed Windows XP.

The windows box has 2 drives, a 15GB with Windows ME and Windows XP on it, and an 80 GB data drive. The 15GB drive failed. So I booted an Ubuntu Linux live CD to run tests on the drive. It seemed to have read errors, so goodbye.

I used the Live CD to move whatever I could, what little was left, onto the data drive. The data drive had 4 partitions, 20GB each, I emptied one of them and crammed the other ones full. "Move over in the lifeboat, folks."

I tried re-installing XP (which involves inserting the ME disk at one point to prove that no, really, I am entitled to the XP upgrade edition) on the 15GB drive. It took forever to format it, and eventually it hung at the keyboard selection screen. I realised later that this was probably because Windows, for some reason, loses it completely when the keyboard/mouse (PS/2, both) go away and come back. I don't know why.

I think the disk may have started failing when I cleaned the computer a few weeks ago. I found the IDE cable was loose. Contributing factor? Maybe. Besides, it's seven years old. Time to move on.

So I disconnect it and reboot a few times, always coming back to "No hard drive found" after a long time of the BIOS poking around. Eventually I realised that changing Hard Drive 1 from "AUTO" to "OFF" worked much better. (Hard Drive 0 is the first one.) Oh, and it helped to put the former data drive on primary instead of secondary. Being an OEM drive, it had NO instructions on it. Usually the jumper instructions are on the drive itself. I bet they were under that cover... but I realised that it was probably on Cable Select when I bought it, and I mus have left it that way, so I just plugged it into the master interface. That worked.

And then Windows refused to install into the first partition, the one I had emptied out. Why? Because the whole disk was one big "Extended" partition with 4 "Logical" ones inside it. Windows refuses to install into a logical partition. Yay. Luckily it had 5GB free, unassigned space at the beginning. and I convinced the Ubuntu Live CD (yay!) to create a couple of random partitions (5GB and the 20GB that got shot up while trying to install Windows in it).

So eventually Windows was installed in a cramped 5GB partition. Windows updates will probably fill it up. Don't care. Remember, my primary computer is Ubuntu; I use the Windows box for games.

Now I just have to get the updates, service packs, etc. I already installed Firefox, Ad-aware, and zonealarm. AVG anti-virus refuses to install until at least service pack 2. But I can play Septerra Core, which was the objective. My newest save game file survived the crash, too.

Tag: geeky