Thursday 22 March 2007

Named Callbacks

Coming from a Java background I often find myself wanting to override constructors and other hooks. I should really pay more attention to the documentation.

Instead of:
class MyClass
def after_initialize
#Do Something...
end
end

Do:

class MyClass
after_initialize :do_something

def do_something
#Do Something...
end
end

Update 13/06/07:

Using named callbacks better conveys your intention, making your code more readable.
Similar article on The Rails Way

Wednesday 21 March 2007

Active Record Constructors

Do not write your own initialiser to instantiate the object. e.g:
class MyClass < ActiveRecord::Base
def initialize(params)
@params = params
end
end

Instead do this:

my_object = MyClass.new(:params => params)
my_object.save

Or this:

MyClass.create :params => params

The above will automatically save the record to the DB.

Environment Variables in Mac OS X

I've been recently having trouble starting certain java applets on my MacBook. For some reason , once downloaded, the Jar file would not extract or run. After much tinkering I found that the applets would work if I started my browser from the command line rather than the dock.

Similarly I couldn't get certain scripts to work from textmate, but they would work if I started textmate from the command line.

The problem was that the $PATH variable I had set in my .bash_profile was not the same as that set in my ~/.MacOS/environment.plist file.

Certain applications depend on being able to read your PATH and other environment variables. However those set in .bash_profile have absolutely no effect on apps started from the Dock. Instead make sure that the PATH variable in your environment.plist file is in sync with that in your .bash_login by doing 'echo $PATH' in a terminal and copying the result into said file.

Syntax Highlighting in blogger

Eventually (in the far distint future) I want to set up my own server to host rails apps on and perhaps a blog driven by Mephisto - particularly cause you can get good syntax highlighting plugins for it.

In the meantime, I discovered that you can export your current highlighting theme from textmate as CSS, using the bundle command 'Create CSS From Current Theme' (vaguely enough ;) ). Then any time I want to post code snippets here, I use the similarly vague command 'Create HTML From Document'

All you need to do is copy and paste the CSS into your Blogger template.

Beautiful.

Selection List syntax

I dont think the explanation of 'selection lists' in Agile Web Development with Rails(1st ed) is very clear. It mentions:

select(:variable, :attribute, choices, options, html_options)

Where you can then set the default selection with:

@variable.attribute

However this variable is not available automatically, you need to create it yourself. The def in the api is more intuitively described as:

select(:my_object, :method, choices, options, html_options)

You must then create your own class with the accessor methods you need so that in the controller you can do something like:

def action
@my_object = MyClass.new
@my_object.method = params[:my_object][:method]
end

session variable not available

You CANNOT access the session hash from within a controllers initialize() method.

Rails Fixtures with Legacy Database

My setup was:

MySQL Table name: TABLENAME
Rails Model name: TableName

  1. The fixture file has to be TABLENAME.yml (not case sensitive)
  2. fixtures() takes the symbol name of the Model, in this case :tableName (note the first lowercase letter) This is the bit that drove me mad - i was writing fixtures :TableName which appeared to work, but then the test failed if I tried TableName(:foo)
  3. Now you can write tableName(:foo)

Hello and Welcome!

This humble blog serves mainly as an aide-memoire for the Ruby on Rails mistakes I've made, so that the head-against-brick-wall process is less painful next time. Who knows, perhaps as I get better, some of these posts will be useful to others too.