Archive for November, 2006

RMagick   no comments

Posted at 4:12 pm in Ruby

I must have spent a good few hours scratching my head when I was trying to get RMagick to work on Mac OS X 10.4.8.

The problem I was encountering was that Ruby would exit with the error: “Bus error”. This nice handy message seems to be plaguing a lot of users.

The problem, as it transpired, was nothing to do with RMagick, but the installation of ImageMagick. I had followed a guide for obtaining all the dependencies for ImageMagick on OSX, but it turns out that there were more, despite ImageMagick compiling and installing without error.

To cut a long story short, the solution was to install ghostscript, something I had blindly missed as one of the dependencies to ImageMagick, and then recompile ImageMagick.

Written by Jamie on November 10th, 2006

Rails & Keys   no comments

Posted at 7:33 pm in Ruby

Rails was designed with specific names for certain things. Your primary key columns in your database tables are one of these things. Rails expects your primary keys to be called `id`. This is all very well and good until you are building an app with an existing database where the keys are named randomly.

Thankfully there are some clever methods in ActiveRecord to allow you to tell your classes what the tables, primary and foreign keys are. Now comes the annoying part…

If the primary key on your `articles` table is called `articleId` you should be accessing the key using the full name and assigning it with `id`.

@article = Article.find(1)
@article.articleId #=> 1
@article = Article.new
@article.id = 1

More annoying is when you come to relationships. Say your articles are related to a category, so you’ve set up the relationship in your models and your category table has a primary key named `categoryId`. Seems sensible however now the code you see a lot of people writing won’t work. For example when you’re writing your edit form for your article you’d initially expect to be able to do this.

select("article", "category",
Category.find(:all).collect {|t| [t.categoryTitle, t.categoryId]},
:selected => @article.category)

However since the primary key of category is not `id` the code @article.category will return the object_id and not the primary key. Instead you might expect to do this.

select("article", "category",
Category.find(:all).collect {|t| [t.categoryTitle, t.categoryId]},
:selected => @article.category.categoryId)

Now however you’ve got a whole new bunch of problems. Firstly you can’t use this in a partial that you want to use in your new and edit actions. This is because when you have a new article the category object will be nil, similarly if you are editing a category that isn’t assigned to a category you will get the same error (nil doesn’t have the method categoryId). You might say, you do a conditional, but that introduces more code. You will also have problems when the value is sent back in the parameters, the model will be expecting an object and can’t handle the fact it gets a number.

The best way to do it is to forget the related object and concentrate on your foreign key, we must also change the field name to that of the foreign key in our article table.

select("article", "categoryId",
Category.find(:all).collect {|t| [t.categoryTitle, t.categoryId]},
:selected => @article.categoryId)

This code can now be used in our partial form.

Written by Jamie on November 7th, 2006

Will it blend?   no comments

Posted at 9:44 am in Genreal

Who am I?

I’d like to say “I am a young, outgoing, fun person who spends all his time enjoying like to the full”. The reality is that I spend most of my time with my ass firmly planted on my leather^H^H^H^H^H^H^H plastic mock leather chair tapping away at a keyboard.

Why should you read by blog?

I’ll kill you if you stop reading!
Plus… I hope to be able to capture those moments in a web developers life where everything falls apart or on the other hand when everything comes together.

So what am I going to write about?

Writing code, reading code, playing with code and more code. I migiht also add a few other bits in here and there if I get time. I should be writing code you know!

What’s next?

Since I am fairly new to Ruby and Rails I plan to write a bit about why ruby is so great and how rails can be very annoying at the same time as being very useful.

Written by Jamie on November 4th, 2006