Courtesy of Twitter

Archive for April, 2008

Self-Referential Relationships

Tuesday, April 29th, 2008

I’ve seen this question asked time and time again, so I’m going to write a short tutorial about how to do it. The question is self-refferential relationships for a model, often the User model to determine the relationship between two different users. I’ll assume that you’ve already got a Rails application and at least a User model for this. We’ll use a has_many :through relationship to define which users are related to who.

Let’s generate a model for the relationship: script/generate model relationship. This will generate a migration which we’ll create our relationships table with.

db/migrate/xxx_create_relationships.rb

class CreateRelationships < ActiveRecord::Migration
def self.up
 
create_table :relationships do |t|
t.integer :user_id, :friend_id
t.string :relationship_type
end
end
 
def self.down
drop_table :relationships
end
end

And that should do us. Run rake db:migrate to add the table in.

Now we go into our User model and we add in the following:

app/models/user.rb

class User < ActiveRecord::Base
has_many :relationships
has_many :friends, :through => :relationships
end

And in our relationship.rb model:

app/models/user.rb

class Relationship < ActiveRecord::Base
belongs_to :friend, :class_name => "User"
belongs_to :user
end

And now we see if it works:

script/console

>> u = User.find_by_name("Ryan")
=> #
>> u.friends << User.find_by_name("Charlie")
=> #
>> u.save
=> true

Now what if we want to change that relationship field? We’ll add in two methods in to the user model to find the relationship for a specific user.

app/models/user.rb

  def to_i
id
end
 
def find_relationship_with(user)
Relationship.find_by_friend_id(user.to_i)
end

The first method, to_i, will return just the id for the user. The reason why we do this is because in the next method, find_relationship_with we pass in a single argument, user. Now because we’ve defined the to_i method on our User model, this means we can either pass in a user id or a user object to this method, and it will call to_i on whatever we pass in, ending up with an id. When the method’s done, it will return a relationship object which you can then modify.

Best of luck.

Windows XP Service Pack 3 Overview Overview

Monday, April 28th, 2008

After seeing aeriff’s article on Windows XP Service Pack 3 I decided to take a gander at what went in to it. Thankfully Microsoft has a handy .pdf detailing it (with awesome pictures!), scroll down to “Files in this download” and click the download button, because Microsoft is too stupid to make sites where you can link directly to their downloads.

First thing you’ll see when you open up this marvel of a document is the Windows XP Professional logo, complete with not only reflected text, but reflected text in a gradient. How Web 2.0. Too bad they didn’t improve the pixellated logo. It gives you a short abstract of what it contains (not like the title didn’t already do that!), and then you move on to the next page.

You could read the whole legal blurb and let your mind melt at the words within it, but I’d recommend skipping this.

Oh look, an index! A whole 10 pages!

And now we get to the Introduction. The introduction explains how Microsoft so awesomely works incredibly hard to improve their operating system (and I don’t doubt they do). You can read how apparently awesome Microsoft is (and I don’t doubt they aren’t… in their own minds), or you can direct your gaze over to the awesome funnel on the right hand side. Here we have three spheres, done up in what appears to be Fireworks or Photoshop, coloured purple, green and red and complete with stroke, inner shadow (actually, it’s bevel & emboss) and drop shadow. Then we have the piece de resistance, the funnel itself. A very light blue with a thick darker blue stroke and an even thicker (but less opaque) black outer glow. What a work of art! Let’s not forget the arrow, which is quite obviously bevelled and embossed, pointing to the large text Service Pack. Finally, to make the whole diagram look almost 3d there’s a very fainy shadow for the funnel, beginning at the base of the funnel to the top of the “Service Pack” text. This should be hanging in the Louvre.

On the next page we are not blessed with such fantastic artwork as the Funnel of Updates, but instead we’re given another huge block of text. It mentions that it does not upgrade it to Vista (duh, that would not cost the consumer money), and does not include IE7, which I found interesting since I believe this was initially released through the automatic updating process. Perhaps Microsoft actually listened to the Mozilla Foundation/Opera/elinks? It links you to a knowledge base article #936929.

The next few pages lists what’s been introduced into Windows XP through updates. WPA2 is something that caught my eye, and absolutely everything else on this first page seemed to be wankery.’

The next page I wanted to look at was page 8, with the huge dialog box open showing how well the Administrator account local security settings is now documented, which it probably should’ve been before the OS was released. Now anyone playing around with this option should either 1) know the consequences if they mess it up and 2) know what they want to know… making this document irrelevant. And it even links you to an on-site address, purportedly giving you more information about the setting.

You don’t have to provide a product key when upgrading, but it will install the “Genuine Advantage” tool, by the look of things.

Won’t be something I’m installing any time soon, I’m running Ubuntu :)

And We’re Back… For Another Year

Tuesday, April 22nd, 2008

After a monumental fuck-around by Telstra, I have an internet connection. Whilst I was being fucked around the domain expired so you all saw that pretty page. Here’s a little rant I wrote when I didn’t have any internet:

On the 10th March, this year, I phoned up Adam Internet and signed up with them, and then gave TPG the required 4-weeks notification that I wanted to disconnect from them, which I thought was bullshit, but it needed to be done so I could get better internet speed: 24mbits from Adam compared with the 1.5mbits from TPG.

So the 10th April rolled around nice and quickly, and kind of snuck up on me. I was under the impression that I cancelled it for the 11th and not the 10th. I only realised this when my internet was disconnected at about 8:45am on the 10th. Within minutes (or seconds?) I was bored out of my fucking mind. I couldn’t do anything. I couldn’t google how to make Ruby and MYOB play nicely, I couldn’t even go to whitepages.com.au to look up the number for GameTraders. Fantastic.

Then I remembered that, usually, when ISPs set up new connections for you that they send you out a router. Adam didn’t do, and still hasn’t done, this. I paid for the router ($199.95) so in legal terms I do believe that makes it mine, regardless of what they told me next when I phoned them on the Friday (11th).

After a 10 minute wait on the phone listening to their awesome soundtrack selection and all the hyped up paid-to-tell-you-how-awesome-Adam-supposedly-is telling me how awesome Adam supposedly is, I get answered by a real person, Jarred. He tells me that “there are still codes on your line” and that “we don’t send out routers until we make sure the connection works, so we don’t have to chase up our routers”. I’m drawn into a stupor. I ask him when the connection will be up and he tells me “5-10 business days from the day the codes are removed from your line”. I paid for the router, it should’ve arrived a few days after I had paid for it, that is if I’m not mistaken about how the whole buying things over the internet works. I thank him for his time, hang up and get a little frustrated as I’m sure most of you would too, realising you had no internet for the next 5, possibly 10, business days. The internet is where I work, and without the internet I don’t get any money because I simply cannot do a large portion of my work without it.

On Monday I phoned up JustComputing to organise a server for SA Window Cleaning, the client that I’m currently working for. I give them the specifications and tell them that there’s no rush on it. It was ready on Wednesday, and that’s a good thing. It took them 2 days to source the parts, build the box, install Windows XP on it (I’ll be removing this) and get me to come and pick it up.

Now tell me why it takes a Goliath-sized organisation 9 days to move a cable from one box to another, which is probably all it takes in regards to my internet connection, yet a small 5-man computer store can make a new computer for me in two days.

This is the strangeness of time, and I have a theory about it. The larger the company, the longer it takes for something to get done. Let’s use Telstra as an example. They have the money, manpower and equipment to do a lot of things that they simply haven’t done because they couldn’t care less, and it is not critical to their survival in order to do these things. One of them is to roll out decent broadband (at least 256k) to the people living in the hills, such as my friend Joe who is still on dialup, in 2008. It’s not critical to Telstra’s survival to do this. It’s also due to the levels of bureaucracy that a thing-to-do needs to go through. In Telstra you’ll have the callcenter minion, and then his boss, her boss, his boss… and so on.

Now lets take a small operation like JustComputing, where every customer is critical to their survival. They will get things done because if they don’t get things done it means they don’t get any money. They get things done quickly, efficiently and without fucking you around with bullshit excuses like “there are codes on your line”. In JustComputing, if something needs approval there’s one level of bureaucracy to go through, and that’s just the one boss. Everyone reports to the same guy and most of the guys can operate independently of him.

I don’t know where or when a big business crosses this line of no longer caring, but it’s crap. You should treat everyone equally, from your employees, right down to their minions and then the customers, the critical part of your business. Making them wait on the phone is only going to piss them off. They will not be reasonable when you pick up the phone. Do you have long wait times for your customers? Hire more people. The more customers walking out of your store with a smile on their face and nothing in their wallet means more in your wallet and a bigger smile on your face at the end of the day, partly due to you not having to deal with stressed out customers. Treat your last customer like you treated your first, and you’ll succeed.

Whilst not having the internet for 12 days, I wrote some more of a tutorial, worked on SA Window Cleaning (mentioned above, and now at the point where I’m ready to give it to them) and got really, really, really good at Guitar Hero.

Oh, and the router still hasn’t arrived.

A HTTP Story

Thursday, April 10th, 2008

A friend asked if I wanted to go to a party and I 202, and said 200. When we got there I asked for a glass of water, only to be given a 400 instead. So I asked for a Coke and the reply was 402, so I 202.

My phone rang but all I got was 206, thanks to the totally rad phone coverage in the venue. So I limited myself talking to people around me. Ended up talking my friend’s girlfriend who complained about her boyfriend’s 411 (or possible 410), and her suspicions he may be 101. I mentioned about my 413 and she gave back 402. The boyfriend overheard and we got into a 409, he said what I was doing was 406 (or was that 405?). He said that he’d be 303′ing from now on, and she said she’d already 302 another. I said that I had 300, to which she shrugged.

I grabbed some appetisers from a passing tray, only for later on that night to have a 500 and I 502′d the appetisers. The party was a 417!

And this may help you to understand what I’m talking about.

Approval Sucks!

Thursday, April 10th, 2008

I removed comment approval on the site, so now when you post a comment it will show up immediately, rather than having to wait for approval. Yeah, it may be abused, but it’s really easy to switch it back.

On a completely unrelated topic (due to the fact I’d have to go to posts and click the “Write Post” button again), another telemarketer called today and I gave her that old “I’m a government agency, don’t call this number”. Hook line and sinker. “Oh, very sorry sir. You will never be contacted again by this campaign.” “I would prefer never again.” I replied shortly before she wished me well and hung up. 100% success rate.

Epic fail from Epic

Monday, April 7th, 2008
"The quick brown fox jumped over the lazy dog".split("").each do |a|
string << "0" if a[0] < 100
string += a[0].to_s << "000"
end

The above code is probably similar to what Epic uses to generate the server descriptions of their UT3 servers. My friend Devastator ranted about how bad these servers were to configure, thanks mainly to the ServerDescription and the insanely crappy way of configuring the entire server, containing the whole configuration in a .bat file (see bottom of post)

So I begun to write him a ruby program that would do it. At the top of the post is my code that works out the correct server description code for UT3 servers. What almost looks like UTF16 is actually anything but. Let’s go through what it does:

  1. Iterates through all the characters and does this:
  2. Finds the ascii value.
  3. If the ascii value is less than 100, it will add a zero (0) to the string.
  4. It then adds the ascii value to the string.
  5. It then adds three zeroes (000) to the string.
  6. Repeat until all characters are done.

Then it gives you something like this:

084000104000101000032000113000117000105000099000107000032000098
000114000111000119000110000032000102000111000120000032000106000
117000109000112000101000100000032000111000118000101000114000032
000116000104000101000032000108000097000122000121000032000100000
111000103000

I added line breaks so it wouldn’t break my blog. Did I mention the whole config file has to be on one line? No? Well guess what? It HAS to be on one line, else you break it. Want to know what it looks like?

D:
cd "Games\Unreal Tournament 3\Binaries\"start UT3.exe server DM-Deck?Game=UTGame.UTDuelGame?GameMode=0?minnetplayers=2?maxplayers=2?
mutator=utcomp3v02b.UTMutator_utcomp3v02b?PureServer=0?timelimit=10?goalscore=0?
ServerDescription=08600009700010800010400009700010800010800009700003200008500
0084000051000032000083000101000114000118000101000114000032000049000?
bPlayersMustBeReady=True?bAllowJoinInProgress=True?bAllowInvites=False?bUsesPresence=False?
bIsLanMatch=True?bIsDedicated=True?bShouldAdvertise=True -Port=7777 -nohomedir -unattended

Line breaks added again for non-breakage.

Ugly, huh?

Oh, by the way if you want to the generator, CLICK ME. You’ll need Ruby to run it, type ruby ut3-config-generator.rb to run it.

Local Gem Repository

Friday, April 4th, 2008

Wow, three Rails-related posts in a row. Sorry for people reading this blog for other things :)

Today’s handy hint is how to get a local copy of all the gems on Rubyforge, so if you want them they’re right there and ready to go. Handy if you don’t have an internet connection.

#!/bin/bash
sudo wget http://gems.rubyforge.org/yaml -O yaml
sudo rsync -avu rsync://rubyforge.rubyuser.de/gems gems

is the code I use. I put it in a file called update_gems in my /var/www directory (default root dir for Apache on Ubuntu) and chmod +x it. From there, I run it and it firstly downloads the yaml file from gems.rubyforge.org containing the specification of all gems, and then synchronises the gems folder with the remote gem repositoy hosted at http://rubyforge.rubyuser.de.

So simple, so elegant, and so fast (apart from the initial multi-gigabyte download).

I keep a copy on my laptop.

unintialized constant Gem::GemRunner

Thursday, April 3rd, 2008

/usr/bin/gem:23: uninitialized constant Gem::GemRunner (NameError)

threw itself upon my screen, shortly after my upgrade on one of my boxes from 0.9.4 to 1.1.0. I didn’t know what went wrong, but I hazarded a guess at it being something to do with the gem executable. A short command later and all things are flowing nicely:

sudo rm /usr/bin/gem && sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

Hope this can help other people, because I know how many times this question has been asked elsewhere.

Rails 2.1: Migration Timestamping

Thursday, April 3rd, 2008

Rails 2.1 is right around the corner, and with it come a few new changes, and one of them is migration timestamping. Instead of adding a number before the migration, for example 001_create_forums.rb, it now adds a UTC timestamp before it, for example 20080403021817_create_forums.rb.

Now, you may think this is a good idea because it will stop the problem of conflicting version numbers on migrations when two developers are working on the same thing, which happens frequently and even there were a few cases at SeaLink where we had this problem and spent some time sorting it out. With the new timestamping it requires that two developers create migrations at precisely the same second, which is less common to happen than creating migrations with the same version number.

But then we get to the tab-completion. Many developers use the console to do their work, or to make small changes to migrations quickly and easily. Tab-completion is a lifesaver and it fills out the name for you. If you were using vim.ruby and the current version of rails, you would type :e to open the file (assuming you’re already in db/migrate) and type 001 and press tab, it would auto complete the name and you can carry on your merry way. Now think if you created two migrations, each 5 minutes apart on this new Rails 2.1 system. You would have to first type :e and then 200804030220 and then <tab> to get it to tab complete, which is a pretty large number to remember.

I myself don’t mind this new change, as I open migrations using Netbeans 99% of the time. There was a lot of heated debate on this in #rubyonrails recently, and there was little talk on the actual ticket which was accepted rather quickly; not that I’m saying that’s a bad thing. The changeset is also available.

Fool!

Tuesday, April 1st, 2008

Yeah, I had an april fools joke page that said this site had been confiscated by the police. That was my fun for the day.

I also sticky noted the kitchen.

What’d you do?