Posts

  • Innerleithen TT Series 2008 Round 1

    Fed up of riding boring, non-technical courses? Then get yourself along to the Innerleithen TT Series!

    The course is a spanker, climbs most of the way up the black before taking the shorcut across to the crop-circles. Then it’s round some of the old-school singletrack which I hadn’t ridden since a few years back with Pete Laing. After this, the fun really starts! Down the downhill pushup track (very gravelly), down The Tunnel, down New Luge, out onto the bottom of Old Luge. Then down The Deer Hunter, eventually finishing with the ski-jump into the arena.

    I decided to ride there for the first round, a hugely sensible idea after 3 weeks off the bike. I blasted the climb, loving the slightly damp technical bits. Unfortunately, when I hit the old-school part, my right calve locked up, and I sat in the heather for a while waiting for it to ease off. It didn’t, so I had to suffer it for the descent (and most of the next day too). Was still loving the sheer awesomeness of the course.

    Organised by “Helen Findlay”:, there are another four rounds, roughly the last Tuesday of the month for July, August, and September. Check the web-link at the top for more details.

  • 10 Under the Ben 2008

    Team Macchiato headed to Fort William a few weekends ago for the 2008 edition of 10 Under the Ben 10 hour endurance race.

    Part of the entry for the 2008 race was a ride up the Aonach Mòr Gondola to register at the top station. Unfortunately, the race was rather over-sold this year, so we were denied our Gondola ride. Maybe for next year No-Fuss would like to limit entries, but that would really go against their ethos. Saying that, the member of their team that told us this seemed rather ‘no-fussed’. Not an impressive start.

    Luckily we had alternatives available; I nailed some pasta in the “Glen Nevis Campsite”: http://www.glen-nevis.co.uk/, while Vo and Tracey went into town to grab some from a fast-food outlet.

    Saturday (race day)

    A good sleep, then up early for registration meant we bagged a great spot for our pits, not far from the transition, and close-enough to the course to see people riding the last section of singletrack.

    Vo did the first lap, a setup we both enjoy as it takes the pressure off. The course started with the usual hugely boring and draggy fire-road for the first few miles, followed by the railway track, a super steep climb, then the fun bits, full of rocks and marginally tech-stuff. The newly-cut natural section was awesome, I thoroughly enjoyed buzzing through there in the big ring. ‘The Wall’ also featured, one of my favourite course-features, mostly due to it’s mincer-baiting characteristics.

    Vo and I had decided we would do 5 laps each, as since we wouldn’t be competing for the podium this year, there was no point in one of us doing a greater number of laps. Anyway, we had a booking at a restaurant to make!

    We finished up 16th, the fastest team with 10 laps. Another lap would have put us in seventh, or thereabouts.

    Sunday

    On the Sunday we (or me, with L plates on!) drove back to Edinburgh, but not before a stop in Glen Etive for some sunbathing and a swim in the river.

    Great weekend (apart from the gondola fiasco)!

  • JQuery on Rails

    Now, I know I am totally late to the party on this one, but how much does JQuery rock??

    For several years now I have done all of my JS stuff using the Prototype and Scriptaculous libraries. I think they’re pretty awesome tools, and certainly being part of the standard Ruby on Rails package has helped them gain a lot of respect and usage.

    Today I had to do a fairly complex piece of UI prototyping that involved a lot of complex JS. It took me about an hour to do it with Prototype, but I wasn’t completely satisfied with the results. Some of m fellow ZoeCity devs had been talking about JQuery for a new project we’ve just started, so I thought I’d give it a bash. 20 minutes later I had a functional better implementation, with less, neater looking code.

    What About Rails?

    As I’ve already mentioned, Rails ships with Prototype, so how do you go about using JQuery in these places? Enter JRails by aaronchi. This is a Rails plugin which replaces all the regular Prototype utilizing helpers with JQuery. Sorted!

    JQuery is stupidly easy to learn, especially for anyone from an OOP background, so you should definitely give it a try on your next project.

  • Whyte Enduro 2008 Round 2 - Drumlanrig

    After three weeks off the bike with a(nother) knee injury, I decided to take my return to racing easy. The 50km Whyte Enduro at Drumlanrig was a superb choice; terrain I love, no pressure to do well, and a chilled-out atmosphere.

    I decided I was definitely going to take it easy, so took the start easy, spinning away at my gears while others stomped on the pedals up the first fire-road. This payed off pretty quickly, as people started going backwards as soon as the gradient kicked up. After the initial climb it was a short fire-road descent to the middle section of fast rooty descents.

    I love these descents, especially when I can use the smooth lines to hold speed. Unfortunately traffic was a bit of a problem, with me nearly crashing twice taking risks to get past people. It never ceases to amaze me how many riders obviously don’t ride technical trails with roots outside of racing. None of these guys were Scottish, so maybe they need to visit the north more often!

    On the second lap I didn’t take a bottle. Mistake! I stopped for a few minutes to give a guy a pump, and pulled in to let Nick Craig and Will Bjergfelt past.

    Third lap I was starting to feel the heat a bit, did I mention it was 25C plus? Lovely! I caught Vo who was doing the 75km on this lap, and sat just off his wheel for the rest of the lap.

    As soon as I started the final lap I could feel the dehydration hitting me. I emptied the bottle I had just taken, but needed more. Luckily Iain and Janet (Nimmo) were having a wander round the track, and gave me some more water, total stars! I had dropped Vo, but later on I had really bad cramps, so he passed me again while I was on the ground stretching the cramp out of my legs. The rest of the lap was a bit of a struggle, and I was pretty shaky at the finish.

    16th, not to bad after time off the bike, at least I was happy with it. Hopefully I’ll get more riding in over the next few weeks.

    Cheers to Vo for driving.

  • End of an Era, Start of a Dynasty

    Close followers of my equipment turnover will have noticed that I’ve been riding my Kona Kula Primo for almost three years now. Time for a change. And what better to replace it with than another!

    Almost. This time I got the 2008 version of the same frame, in the Kula Deluxe colours, which features some improvements for the first time in 4 years:

    • No V-brake bosses (about time).
    • 73mm BB instead of 68mm.
    • No gusset.
    • Lots of nice profiled (hydroformed) tubes.

    Otherwise the measurements are exactly the same, and it’s still sub 3lbs. It’s even almost the same color. Looking forward to riding it, when my knee’s better. Oh, and yes, it shall be getting some white SIDs when they’re available.

  • Mod_Rails aka Passenger Migration and Review

    Deployment of Ruby on Rails apps to a production environment has always been a huge pain in the backside. The most common setup seen these days is to run a proxy in front of a cluster of “Mongrels”:, which leaves you with a lot of processes to look after. Not ideal. Recently I got excited about using Jruby and the Glassfish Gem to run apps, but this is something I’m going to come back to at a later date now.

    So the new kid on the block is mod_rails (otherwise known as ‘Passenger’) for Apache. So how can this be any easier?

    • Install is a case of installing a gem, running a command, and following the on-screen instructions.
    • Rails now runs within the realm of Apache, so we can start and stop it as if it were one process.
    • This spawns and manages a number of Ruby processes up to a maximum, as demand requires.
    • Restarting the Ruby processes for a specific app is simply a case of modifying a single temp file.
    • Multiple Rails apps can share the pool of Ruby Processes, so no more Mongrels hogging resources that could otherwise be used by other apps.

    I run two apps on my Slicehost VPS; this blog, and my training diary. Based on their traffic I was running three and one Mongrels respectively, maybe too many for a 256MB VPS. For Mod_Rails, I decided to follow the recommendation in the documentation of running a maximum of two Ruby workers on the memory I had available. I was running Apache anyway for my public SVN, so the transition was a case of moving my virtualhosts over from Nginx, stopping Nginx and the Mongrels, and restarting Apache. Ludicrously easy.

    Some basic benchmarks showed that performance was on a Par with Mongrel, which is certainly pleasing. One small gotcha that I encountered is that the user which the Ruby processes are run under is the same as the owner of environment.rb in your Rails deploy. So check this is the user you want.

    Capistrano Restart

    If you’re deploying with Capistrano (and why shouldn’t you be?), the add this to your deploy.rb to get your restart rolling:

    
    	namespace :deploy do
    	  task :restart do
    	    run "cd /path/to/app/current; touch tmp/restart.txt"
    	  end
    	end
    

    Barring typos, that should be good to go. I look forward to seeing the reception of this amazing improvement in Rails deployment by the community. Shout out in the comments with your thoughts and/or problems.

    Update

    I had some issues with static files not being served in one of my projects. My solution was to remove the public/.htaccess file that the rails command generates. Hope this helps other’s out.

  • SXC 2008 Round 1 - Laggan

    Just like the last two years, the first round of the SXC was held at “Laggan Wolftrax”: http://www.laggan.com/wolftrax.htm. No worries with the course then, the same fast fire-road climb, followed by the long rocky singletrack descent. But then there was the snow, which had fallen over the day before and morning. Luckliy by the time we got to race in the afternoon, there was only meltwater on the track, but a trip off the track would land you in snow. Fun!

    The expert (yep, I got me an expert license finally!) race got off to the usual fast start, big ringing it most of the way up. Andy Barlow and Hamish Creber both made a good gap on the rest of the field by the top. I got into the descent in a fairly poor position, but wasn’t impeded too badly on the descent. For the next couple of laps I was loving it, doing the climb with Richard Macdonald, dropping him on the descent, then getting caught again on the flat sections. By the fifth lap, I was pretty cold, and was starting to lose my enthusiasm. I also had to stop a few times and pull my knee warmers up.

    On the last lap I saw Alex McLean making up time on me round the top section, so I had no choice but to give it guttie and hope I could hold him off. He caught me just as we joined the bottom section of fireroad, but some sprinting action saw me keep my place for the lap. 13th place, not brilliant, but on-par with the sort of placing I had last year, albeit with only a month in the saddle this year.

    Bring on Dalbeattie!

  • Scotland On Rails 2008 - Day Two

    Today was the second day of Scotland On Rails which I was visiting courtesy of a free ticket from EngineYard.

    Update: They had tea, maybe all conferences should do this!

    Here’s a run down of the talks I went to:

    Keynote – David A Black

    Despite having been ill the day before, David provided a talk on the similarities between programming and music. I’m sure other people will provide more on this, but one difference is that musicians don’t hate those who play different instruments, unlike coders ripping on other coders due to their choice of tools.

    Rails Patterns – Jonathan Weiss

    Jonathan gave a talk on some common rails patterns, here’s a rundown:

    • Long running tasks, video/audio processing etc: Break these off into separate jobs, and run them later.
    • Lots of data: Use a background process to push to a service such as “Amazon S3”:, and use your local disk as a cache in case of downtime or for popular content.
    • Plugins: Don’t blindly use them, always look over the code. A lot of plugins are poorly or infrequently maintained.
    • Dependencies: Vendor everything, Gems, Rails. Saves your developers a lot of time in getting up and running when they join the team.
    • DOS avoidance: Split up your Mongrels into groups, that way multiple slow downloads or uploads don’t consume all your resource. Don’t use sendfile, use xsendfile, free’s up the Mongrels faster. Maybe use another tool to deal with uploads, Merb or CGI.

    Collective Intelligence: Leveraging User Data to Create Intelligent Rails Applications – Paul Dix

    Paul of Mint Digital gave a great talk on how you can mine your user data to provide them with recommendations. Using the “Netflix challenge” data, he showed an example using Ruby and some C libraries. Very clever stuff, I’ll probably be digging into this more later.

    Advanced Ruby Class Design – Jim Weirich

    Jim gave a great talk on designing Ruby classes in a Ruby way. He highlighted some cool tools, including the BlankState class, which undefines all the Object/Class methods normally attributed to a Ruby class. In 1.9 there will be a version of this called BasicObject, from which Object inherits, thus creating fewer problems for classes doing clever things like DSLs where namespaces may collide.

    Domain Specific Languages: Molding Ruby – Joe O’Brien

    Today Joe presented a talk on DSLs. So what is a DSL? Well, you know the absurd nomenclature Starbucks use for their coffees? Venti, Grande, etc. Well, that is a DSL, designed so that when a customer makes an order, the chance of the barista getting the order wrong is pretty wrong, as everything is ordered so that nothing can be missed out. I essence, as someone pointed out, the barista is a compiler.

    A DSL is a mini language, if you will, with “Rake” being one of those that Rails developers will be most familiar with. When designed well, a DSL will bring methods calls and operations up to the level of natural language. Image the benefits of a well designed DSL in showing implementations and getting approval from clients. So, bring your code up to the client, not the client down to the code.

    JRuby on Rails: Up And Running – Charles Oliver Nutter and Thomas Enebo

    Charles and Thomas (THE JRuby guys) presented what was for me, the best talk of the entire conference. Not only in it’s revelations, but the enthusiasm they brought to the subject of JRuby.

    The last time I played with JRuby was a year ago, when it was pre 1.0, and a bit buggy. During the talk the guys officially released 1.1, despite it being available for over a week. To save me a lot of effort I’m just going to list some of the facts regarding it:

    • JRuby is just Ruby.
    • Has a Java port of the Oniguruma regular expressions library, like Ruby 1.9.
    • Compatibility is key in it’s development, Rails, Rake, RSpec etc all run. Full ruby 1.8.6 compatibility. 42k assertions in tests.
    • Java 6 over 30% faster than 5, all good for JRuby.
    • JRuby 1.1 is 2-5x faster than Ruby 1.8.
    • Dynamic optimization makes programs run faster as they run longer.
    • JRuby almost twice as fast as Ruby on a single thread. Threads in JRuby are real, so near-linear scaling across cores!
    • Can take advantage of different profilers available to Java, the guys showed a live profiler demo inside NetBeans using fractal benchmark.
    • Can call Java APIs/classes from within Ruby. Showed off a Swing example where the interface was build through Ruby methods, much nicer than the horrible Java methods.
    • Can dynamically add methods to Java classes, Java won’t see them though.
    • NetBeans supports JRuby and Ruby. Command completion, method completion, method jumping, regexp completion, % completion. Shows RDoc for methods as you type them. Auto complete require classes and subclass completion.
    • Example of Processing , run Ruby algorithms, through the Java lib, hardware accelerated on OpenGL. Games, Nick? These guys reckon someone will do it soon.
    • JRuby database support through activerecord.
    • Package your Rails app as a WAR file, everything in 1 process. Throw into GlassFish, done, instant scaling across multiple processors.
    • JRuby runs in asynchronous mode, can spin off new processes.
    • Ruby 1.9 support on the way, maybe in the next major release.

    Awesome stuff, expect an article on getting up and running with Rails on GlassFish here soon.

    Wrapup

    All in all, it has been a fantastic few days. I’ve learnt lots of stuff and met some great people. Roll on next year.

    Thanks very much to the SoR Team for all their organisation.

  • Scotland On Rails 2008 - Day One

    Today was the first (official) day of Scotland On Rails which I was visiting courtesy of a free ticket from EngineYard.

    Have met lots of great people, as expected. Am rather surprised at the number of non-rubyists, there are a fair number of people who currently work full-time on Java or .Net and are pursuing Rails as a sideline with a view to either going freelance, or ‘sneaking’ Rails in the back door at their employer. They were all impressed that I was a full time Rails developer, and have been since I left University. Maybe it was jealousy, but I can see why it is a lifestyle that many would like.

    Here’s a brief rundown of the talks I attended, and what I took away.

    Keynote – Michael Koziarski

    Many Rails followers will know Michael as Koz, the man on the Rails core you hassle until your patch gets accepted. His keynote wasn’t the inspirational piece I would usually expect as a keynote, but did cover some interesting material as far as code beauty is concerned. In Rails there is some nice code, and some not so nice code, as expected, but it was nice to be shown some of it, and the reasons for it’s existence.

    Pushing Rails onto our Stack: A System Integration Study – Richie McMahon & Maria Gutierrez

    Richie and Maria (was very amused by her accent, is Spanish, with random bits of Edinburgh mixed in) put on a talk about their adventures integrating Rails apps with an existing Java infrastructure. They covered:

    As well as covering XMLRPC in the days pre-ActiveRecord, they also covered working with legacy database schemas. One interesting thing that was brought up is the lack of support in Rails for legacy STI keys. A quick patch seems to have solved it though.

    Handling Long-Running Tasks in Rails – Andrew Stewart

    Andrew gave a great overview of the solutions out there for running background and scheduled tasks in Rails. BackgroundJob seems to come out the best, through ease of installation, setup, breadth of use cases and persistance. Lot of options out there though.

    Edgecase Dialog – Jim Weirich and Joe O’Brien

    Due to some rescheduling, this was earlier than planned. Jim (Author of Rake) and Joe of EdgeCase gave us an amusing but informative three scene play on the benefits of using mocks and stubs in your tests. The examples were presented using Jim’s FlexMock library, which I shall be checking out as an alternative to Mocha over the next few weeks.

    Code Generation: The Safety Scissors of Metaprogramming – Giles Bowkett

    “Giles”: provided the most amusing top of the day, complete with pictures of Jessica Alba and Darth Vader in a sombrero. Apart from the comedy, his talk was a great overview of what can be done when code is used to generate code. In short, code == data && data == code! He highly recommends Jack Herrinton’s Code Generation in Action, which was originally supposed to be a Ruby book, but due to the publishers fears about a Ruby book (this was 2003) the second half of the book is filled with examples in various other languages.

    Migrating to Ruby 1.9 – Bruce Williams

    Bruce presented a great talk on some of the new features and gotchas in Ruby 1.9. The highlights for me were the new hash symbol syntax: { a: 1, b: 2, :c => 3}, and the persistent ordering of hashes. The new lambda syntax is somewhat less verbose than previously, and seems like a step backwards. Be sure to check out the app Bruce used to run code in both languages for comparison: Compare-1-9.

    Bonus Erlang Session – Gordon Guthrie

    Gordon is a (relative) local, having only travelled from Linlithgow. He works for a small two-man Erlang development team. He gave a great overview of why Erlang deserves the attention it has been receiving. Despite not showing many code samples, he certainly managed to whet my whistle for having another go at learning it.

    All in all, an awesome day, despite the early start. Thoroughly looking forward to tomorrow.

  • Rails: Fix has_many through polymorphic has_many Relationships

    Ok, so the title is a bit of a mouthful, but stick with this, the answer to a longstanding Ruby on Rails problem is at hand.

    The Setup

    Imagine you have a setup similar to the following:

    
    class User < ActiveRecord::Base
    
      has_many :albums, :as => :albumable
    
    end
    
    class Album < ActiveRecord::Base
    
      belongs_to :albumable, :polymorphic => true
      has_many :images
    
    end
    
    class Image < ActiveRecord::Base
      
      belongs_to :album
      
    end
    

    So a User has many albums, which have many images. But, albums have a polymorphic relationship, so they can belong to other objects if required.

    The Problem

    The problem comes when we want to fetch all the images that belong to our user.

    Thinking about it, we should just add has_many :images, :through => :albums to the user model, but unfortunately this breaks in Rails 2.0.2 and prior versions.

    The Solution

    Easy, just do one of the following:

    1. Upgrade to Edge Rails, which may carry risks depending on your app setup, but does fix this issue along with adding lots of shiny new features.
    2. Wait for Rails 2.1.0 to be released, if you’re patient!

    Enjoy!