The Initialization Guide

February 8th, 2010 by Radar

For a couple of weeks now I’ve been writing a guide on how the initialization process in Rails 3 works. As of writing, the guide and its accompanying “notes” totals over 10,000 words. I admit that this is currently nowhere near completion, but I am working on it and I need your help. I need people to go over it for me and point out anything that they don’t quite understand or I have stated incorrectly and file an issue on the repository for it. Originally I was going to accept pull requests but they aren’t “public knowledge” and I don’t want two or more people submitting pull requests to fix the same chunk of code. With issues, people can see that “oh, there’s already a typo here.” and move on to finding more errors and punching more holes through it.

So yeah, if you guys could read my guide and file any issues you find with it, that’d be swell!

Computer Woes

February 1st, 2010 by Radar

Today I had to deal with two Windows-based computers’ problems for Mark and Marlene. Here’s what happened.

Mark

Mark has a 4 year old Compaq Presario f500 which occasionally when you switch it on it’ll keep rebooting itself without showing anything on the screen. The fix for this was to turn it off, leave it off for a couple of seconds and then turn it back on. Then the problem went away until you turned it off and on again, in which it would resume. I couldn’t figure this one out. The other problem Mark had with his computer is that when he left it alone the screen would go strange. We figured out this was his screensaver, and changing away from the “Pictures Slideshow” option fixed it. I suspect there’s something in there that isn’t quite right.

Marlene

Marlene’s daughter has a recently bought no-name-brand case with a no-name-brand motherboard in it. Marlene had the computer at her house because she told me about the issue last night and I couldn’t fix it over the phone and her house is easier to get to than her daughter’s. The issue was that the computer wasn’t getting past the first POST screen, the one with the CPU & Memory information. I opened up the case when I got to Marlene’s house and saw that the cable for the CPU fan had a tiny cable tie clip on it that was stopping the CPU fan from spinning. Apparently this stops the computer from getting past that point! I moved it out of the way and tucked it around some other cords to make sure it wouldn’t get in the way again, sealed the case and tested it, it worked and so I thought that was that.

Until 5:30 tonight.

Marlene phones me up. The computer’s making a beeping noise. I can hear it in the background, “BEEEEEEEEEEEEEEEEEEEEEEEEEP”, pause, rinse, repeat. One long beep. It wasn’t doing that earlier. So I trek as far north in Brisbane as I have ever gone to play around with this box. I needed to fix this problem as I felt it was something I did. I get there and sure enough it’s making the “BEEEEEEEEEEEEEEEEEEEEEEEEEP” noise. I take the side off the case and the cord for the CPU is where I put it and that fan’s spinning alright.

I looked it up on Wikipedia and it said that it could have been a memory issue, so I took that out. No change. I took out the graphics card. No change. I took out the sound card. No change. I unplugged the power and data cables to the harddrives and DVD drives. No change.

What the hell?

I was on the verge of giving up so I just put everything back in the case one thing at a time, starting with the graphics card.

CHANGE!

The computer emitted a singular quick beep indicating successful POST and a choir of angels was heard in the distance.

Pieced it all back together and it booted fine. Did the ol’ go-through-and-remove-all-the-spyware-shit-that-builds-up routine and another happy customer!

tl;dr:

  1. Wacky screen after a period of non-use? Try changing your screensaver.
  2. Long singular beep? Ensure the PCI cards are seated correctly.

Why Mocra Is Awesome

January 21st, 2010 by Radar

Mocra is awesome because their employees are some of the brightest and most “switched on” people I know. They come up with fantastic ideas and execute them brilliantly. They even sometimes allow their employees to work on open source during work hours. Putting that much brainpower into a company that small is a genius idea. Yes, they’ve struggled. But what small business doesn’t? It’s still in its early phase and they’ve refined their process down to a tee and are really getting their stuff together. I fully support the company and will remain friends with them all. If you’re looking to hire a Rails company for your new/existing Rails application, hire Mocra.

Now to being completely honest with everyone.

I’ve decided that I want a change. As of today, I am no longer employed by them. I’m not sure how to phrase what went down. I got angry, and left. This had happened before. Later Nic called and asked me to come in to have a chat the next morning. This is entirely my fault. I wanted to leave for a while, to explore what else is out there. With freelancing, I got that power to travel and work with different people. I know the whole “grass is greener” cliche, and I’m aware of that. I need something different, perhaps to only focus on one project instead of multiple at a time? I’m not quite sure. Freelancing was fun, but it’s also very, very lonely. I’ve had a couple of leads but I think it’s too soon to be jumping into another job. I need to realise that, attitude wise, I need to change it. I’ve changed it since NetFox quite a lot with some of that due to a little book called “How to Win Friends and Influence People” by Dale Carnegie. The suggestion to read this book came from no other than Dr. Nic himself. I read it and realised there were some things in there that I wasn’t doing and that would not only improve my life, but the life of the people around me. It did exactly that.

One thing the book does not have a chapter for is anger. When we went to the Ski Trip at Thredbo I got angry with a waitress because I wasn’t served a steak. Everybody else’s order came out, and mine was simply lost in the process. They made a mistake, and I got angry for no real reason. The waitress apologised but the mistake was done! How dare she stuff up my order! She’s supposed to be a professional. I could put this down to the fact that I was tired, but that should not be used as an excuse. As long as I have the ability to breathe I should have the ability to be polite and analyse a situation before showing my emotional response. This is what I need to continue to refine. There’s no showing you how much I’ve improved since the school days, since Netfox, I can only state that I have.

And only yesterday did I lash out at Bo. I had just received an email for my car damage. I had scraped the side of it and destroyed the right mirror by reversing along side a pole at a shopping center. I was angry at myself then, and that level of anger I believe was acceptable for that situation. I made a mistake, and it was going to cost me money. The email said the repair and the service was going to cost $1,500! I tried to bottle it up and shrug it off, but it ate away at me inside. $1,500! GONE! For what? Shortly after I was writing a bash script to change the keymappings for a plugin Mocra uses called Telefrag. This plugin works with Teleport and allows me to type in QWERTY when Bo or Chendo is typing in Colemak or Dvorak respectively, dismissing the need to switch keyboard layouts with a shortcut. The script wasn’t working as I wanted it and Bo suggested something whilst I was trying something else, and I said something angrily at him. This is an unacceptable case of showing my anger, and therefore I failed, again. I was not angry with Bo and he’s not the one who cost me $1,500. It was me. Getting angry like that cost me my job and I fear could cost me future employment opportunities and eventually leave me in a worse state than where I am at now. I need to stop getting angry so easy and over such trivial things. I need to learn how to.

Mocra is awesome, I failed them and am now unemployed again due to my, and only my, actions. I will fix this, I promise you. This post needs to exist so that people can show it to me, remind me of it, snap me out of it. I will always remember how bad I felt writing it, but also how much of a relief it was to get it off my chest. Yeah, future employers may see this. At this specific point in time I am still human, and therefore still have emotions. This is me describing my journey of controlling them for self-betterment.

Thanks to those who have offered their support over the past 24 hours (friends & family), you have all been brilliant and without such a group I would have probably done something drastic. If you guys ever need anything, I am always available for a chat, as you have been for me. I don’t care if it’s 4am. Call me if we need to talk. I’m there.

Seriously, hire Mocra. They have been fantastic, and I know that they have, can and will succeed.

Ruby 1.9.1 & Friends: 11 Months On

December 5th, 2009 by Radar

I admit that the title would be cooler if this were “1 year on” but I couldn’t wait. Sorry.

Back in January I wrote a post about installing Ruby 1.9.1 on my Mac OS X box and the issues associated with it. The “end goal” was to get rboard running on it under passenger. This was painful back in January! It’s so much easier now!

Unfortunately, there’s still this bug which effects you if you’re on Passenger and 1.9.1. There’s a patch for one of the answers there and that’ll get your application working, but I’m of the belief that you should not ever have to patch a gem and especially the Ruby source.

I want to know: Have you tried running your app on 1.9? Have you encountered any showstoppers?

Quite a lot has changed since January and I’ve decided instead of updating that post, I’m going to go through the entire process again. This is because I would like to show how far Ruby has come during the time since I have written and updated that post and that running your application on Ruby 1.9.1 isn’t as much hassle as people make it out to be.

This time I will be installing it all on an Ubuntu box, since that’s more than likely the location where you’re going to be deploying your app’s code to. I’ll be using Ruby 1.9.1p243 and Rails 2.3.5.

For those of you who don’t like long blog posts here’s the script if you want to install everything without reading through the cruft. Running this script or bash -c “`wget -O – frozenplague.net/boris`” on your server will install the bare-basics:

  • git (latest version)
  • apache 2
  • mysql 5
  • ruby 1.9.1
  • rails (latest gem version)
  • passenger (latest gem version)

Then you’ll be able to setup your Rails app. I can’t do that for you, sorry. My scripting-fu is not that good.

Thanks to Ben Hoskings for the inspiration for this script.

The Long Road

To install Ruby 1.9.1 from source on a base Ubuntu system there’s a couple of pre-requisites. Let’s cover why they’re required.

build-essential

Includes stuff that is, well, essential to building things, like Ruby and the other dependencies.

libssl-dev

Needed for when we go to install Passenger. If not installed you’ll get no such file to load -- openssl because Ruby didn’t install openssl because this dependency was not installed. Fun times were had.

libreadline-dev

Needed for launching script/console. It’ll complain no such file to load -- readline when trying to launch it.

zlib

When you go to sudo gem install rails you’ll get no such file to load -- zlib. This error message is not helpful but it means you need zlib1g installed: sudo apt-get install zlib1g-dev

Now you’ll be able to download and install ruby:

mkdir ruby
cd ruby
wget --progress=bar ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p243.tar.gz -O - | tar -zxf - --strip-components 1
./configure
make
echo "Need your password to install Ruby:"
sudo make install

And then finally your gems:

cd ~
sudo rm -rf ruby
sudo gem install mysql passenger rails
sudo passenger-install-apache2-module

I needed to patch Ruby + Passenger with this patch, as when I went to log in to rboard it gave me the error at the top of that page.

Other than that, all of this works perfectly fine on my bare-bones Ubuntu box. I was even able to get rboard running on this setup!

So again: Have you tried running your app on 1.9? Have you encountered any showstoppers? Really keen to hear your stories!

A Day Without Talking

November 23rd, 2009 by Radar

So I’ve just been to yet another fantastic Railscamp over this weekend and I really enjoyed myself. Well done to the organisers.

As per tradition, however, I have come down with yet-another throat related issue, this time I suspect (using my e-doctor skillz) it’s Laryngitis. On Saturday it was a little bit of effort to speak but I could still do it, yesterday was slightly more effort and today… well: I was putting in as much effort as I would to shout my words in order for them to come out as barely audible-whispers and cracking, broken speech. A friend, Lucas, even gave me the nickname “Squeak” by how often my voice cracked under these conditions.

I have just tried saying “My name is Ryan Bigg” and it comes out in a hoarse whisper. Just tried counting to 10 and I only was able to say 1, 5 and 7 correctly. As an extremely vocal person, this is going to suck bigtime.

So, as part-experiement and part-fucking-laryngitis(??) I’m going to go tomorrow without speaking any words. I will go about my usual routine of getting food + coffee for Chendo from the shops on the way to work, doing my job, getting lunch & dinner, having conversations with people, but all without speaking. I don’t want to exhaust myself by trying to speak. I am already exhausted today, even after 9 hours of some of the most beautiful sleep I’ve ever had.

For prep for this experiment I’ve already ordered dinner from Halim’s (Indian Restaurant) up the road using only pen + paper I bought. I wrote common words + phrases like “I am unable to speak due to Laryngitis. Sorry for the written prompts”, “Yes”, “No”, “Thank you”, “Lamb Rogan Josh”… etc.

This made me think of good-to-have iphone app. A common phrase book for those between us who cannot speak, either due to throat inflammation or those who are mute. You’d have the main screen which would be a 4 x 4 grid of common phrase words, kind of like the home screen, and you could tap them and bring them up on the screen and show it to the person. Then you could add your own as well. I think that would be really useful. What do you think?

Scoping by locales

November 8th, 2009 by Radar

Today in #rubyonrails, kiwinewt asked:

How can I have a model with a text field and have that text field in multiple languages?

To which he meant that he has a model and he wants different versions of text displayed based on whatever the locale is set to. This is quite the common question in the channel and previously I’ve drawn blanks, but today I had a Moment of Clarity +10 and coded up something amazingly simple.

It has two parts. The first is the code in the model:

class Page < ActiveRecord::Base
  def self.with_locale(&block)
    page = scoped_by_locale(I18n.locale.to_s) { block.call }.first
    page ||= scoped_by_locale(I18n.default_locale.to_s) { block.call }.first

    raise ActiveRecord::RecordNotFound, "The page you were looking for does not have a version in #{I18n.locale}" if page.nil?
    
    page
  end
end

And the second is how you use it:

  @page = Page.with_locale { Page.first }

Now if you set I18n.locale in your application and use with_locale it will automatically find records based on that locale.

Where did I put that puts?

November 4th, 2009 by Radar

This is the question I ask after I’ve just finished a massive debugging session and I run the tests and halfway through there’s something vague like “S3″ printed out. So I do a Cmd+Shift+F looking for that string and of course it doesn’t exist. What’s a guy to do?

Well, at Mocra we put this in our config/environment.rb file (although a better location would be in a required file located somewhere in lib, probably named debug.rb):

# Print the location of puts/p calls so you can find them later
def puts str
  super caller.first if caller.first.index("shoulda.rb") == -1
  super str
end

def p obj puts caller.first super obj end

And when we don’t want it we comment it out. This will give us the exact location of the puts so we can track it down and remove it.

Size != Count

October 30th, 2009 by Radar

When using a has_many :through in Rails with a counter_cache an interesting thing can occur. When you call size on the association, it can return a seemingly incorrect number. This is caused by the following code in activerecord/lib/has_many_through_association.rb:

def size
  return @owner.send(:read_attribute, cached_counter_attribute_name) if has_cached_counter?
  return @target.size if loaded?
  return count
end

It’ll reach the counter_cache column which could be incorrect, giving you all the objects returned when you look up the association, but an invalid number. This was only in my tests where data was being created through Machinist. Just watch yourself for this, this is the second time it has caught me, and ideally the last.

Connecting to Multiple Databases Using ActiveRecord

October 17th, 2009 by Radar

You can call establish_connection with the key that points to another database config in your config/database.yml file

class Person < ActiveRecord::Base
  establish_connection(:hr)
end
class Ticket < ActiveRecord::Base
  establish_connection(:bug_tracker)
end

If you have a whole bunch of models that need to connect to another database:

class HR < ActiveRecord::Base
  establish_connection(:hr)
end

class People < HR
  # ...
end

class Resource < HR
  # ...
end

Fixed By Rails 3

October 9th, 2009 by Radar

Today I launched a sister site to fixedbytm2.com which is fixedbyrails3.com. Here you can list your gripes and wishes for Rails 3, as well as the new feature of being able to vote on them! Enjoy!