Friday, December 14, 2007

Beware reserved words in legacy databases

This took me a damn long time to figure out.  My rails app is using a legacy database for part of its data source.  It was quite complicated: I was using multiple databases in Rails, namespaced models, non-standard table names (with set_table_name), and non standard primary keys (with set_primary_key).  So when things like ActiveRecord associations didn't work, I figured that it was because I was trying to force this mess into the Rails conventions and just having no luck.

Turns out, it was something simple.

I continued to strip away more and more of the messy configurations -- got rid of associations, moved the tables into the same database temporarily, renamed tables and primary keys, etc.  But still no luck.  Just trying to access a simple attribute from the model in the console would result in this incredibly helpful error message:

  undefined method `generated_methods' for 8:Fixnum

What?  And to make it worse, a Google search for this error turned up nothing.  Nothing as in "no results found," not the typical Google result junk.  Well, I won't tell you how long it took me to figure out this problem, but it came down to this:  One of my legacy tables (the main one) had a column name in it called class.  It's supposed to represent a student's class standing in school.  Well, of course, "class" is a reserved word in Ruby, so therefore all hell breaks loose in situations like this.  Dammit.

Is there no way for Rails to check for this?  Why wasn't there a helpful "Cannot use reserved word here" error instead of the useless one above?  Rails was able to get the column listing from the table just fine.  The problem only happened when it tried to do its "Rails magic" and auto-generate all of those handy accessor methods.  So why not do a quick check on the column listing for reserved words?

Either way, it's fixed now with a simple mapped column in the DB.  Time for the weekend.

Wednesday, November 28, 2007

Rails + pubcookie... finally

Well, I've finally gotten Pubcookie authentication working with my Apache + mongrel cluster configuration.  After talking to a guy on the Pubcookie team, he pointed me to this invaluable resource:
http://www.washington.edu/webinfo/case/zope/

It turns out that most of it is unnecessary if you've already followed the instructions for setting up the mod_proxy_balancer as found here.  The missing key to get Pubcookie working was the mod_fba module that is linked in the case above.  Here is what I did to get everything working (after following Coda Hale's process above):

1. Add mod_fba to your Apache setup:
% wget http://rici.ricilake.net/src/mod_fba.c
% apxs -c mod_fba.c
% apxs -i mod_fba.la 

2. Edit httpd.conf to load that module:
LoadModule fba_module modules/mod_fba.so 

3. Add the configuration for FakeBasicAuth to your myapp.common file:
FakeBasicAuthEnable on
FakeBasicAuthType authtype 

4. Since I want the entire app protected by Pubcookie authentication, add the AuthType directive to the myapp.proxy_cluster.conf file to require a login for everything sent to the mongrel cluster: 
<Proxy balancer://mongrel_cluster>
  BalancerMember http://127.0.0.1:8000
  BalancerMember http://127.0.0.1:8001

  AuthType authtype
  require valid-user
  PubcookieAppID "myapp" 
</Proxy>

5. Now, you'll be able to get access to the HTTP_AUTHORIZATION server variable in your ruby code.  This will be in the form of "Basic" followed by the user's login information encoded in Base64.  This line of ruby will extract out the username only for you to use in your app for authentication purposes:
userid = request.env['HTTP_AUTHORIZATION'].split(' ')[1].unpack("m").to_s.split(':')[0]

Monday, November 26, 2007

Is rails worth it?

I'm starting to regret the idea of using rails in this environment.  After spending a week to get apache + mod_proxy + balanced mongrels working, now I don't even know if I can use pubcookie authentication with rails at all.  I don't really have a choice on that one.  Starting to wonder if I should have just gone with an MVC framework in PHP, like Cake or Symfony.

Friday, November 16, 2007

Decided to start a blog

Well, I'm starting to build a new rails webapp for my office that is in pretty in depth... and I decided I needed a place to write some things down as I did it!  Who knows, maybe this will become a useful resource at point -- if no one else but me.