Ruby On Rails Problem: Lazy Loading

I have a model that works something like this. I have a User object. Each User has a Profile. Each Profile has a Picture. Well not always. At creation there is no Picture and the user could choose to never set one.

Prior to my latest upgrade of RoR I would use a line like this to check if there was a picture.

if @user.profile.picture.nil?

But when I went to 0.14.1 this started giving me an error, “uninitialized constant Picture”. I define the relationship in the model by saying:

class Picture < ActiveRecord::Base
belongs_to :profile

and in Profile

class Profile < ActiveRecord::Base
belongs_to :user

has_one :picture,

:dependent => :destroy

The addition of the :dependent => :destroy is new to this version. So how do I specify this relationship knowing the picture may not be there? Do I have to not use belongs_to, has_one?

Technorati Tags: ,

Java vs. Rails

Joey over at Tucows has an article about where all the cool stuff is happening and how it isn’t happening at Microsoft.

For an example he shows a big chunk of XML and big chunk of Java and these three lines of Ruby:

01 class Order < ActiveRecord::Base
02 has_many :items
03 end

The jist of the article is that MS sucks, which is always a truism. But being the geek I am, I have to point out the comparison is a little unfair. What he’s actually comparing is some Java frame work for DB to Ruby on Rails, not just Ruby. The magic that lets you write three lines of code is in ActiveRecord.

That’s not to say the XML isn’t stupid and could be fixed with Convention Over Configuration*, which is what RoR does. I used to do WebObjects programming for Apple and the configuration to map database tables would probably look that bad in XML but you never wrote XML because Apple had a tool to do the configuration. I like RoR’s method better.

* I can’t find a definitive explanation of Convention Over Configuration to link to. Mmm….

Technorati Tags: , ,

Experienced Coders

I was reading some of the links off Particle Tree’s article on Improving Your Coding Lifestyle and found this article on The Value of Experienced Coders.

A couple of things struck me as someone who has done this programming thing for 13 years now. One was remembering one of the best companies I ever worked for, Pervasive Software. The only reason I voluntarily left them was to go work for the holy grail of Mac programmers Apple. But in Douglas’ article he talks about there being no career path for coders.

The next time I noticed this phenomenon was when Ed, a good friend and excellent coder, was encouraged to move into a management position in order to advance his career. The company apparently had no career path for a really good developer with more than five years of experience. You simply reach a point where there is no hope of any salary increase beyond the normal cost of living adjustments, no matter how capable you are as a software developer.

Pervasive had a career paths for developers and there were two, one for people willing to go into management and one for coders who didn’t want that. And the top managers’ salary was the same as the Senior Scientist’s. These guys knew what they were doing to motivate their employees.

The second thing that struck me was his comment about how the software industry is built on inexperienced programmers. The system is set up so the people writing your code have the least amount of experience.

One consequence is that a large part of the coder workforce is recently out of college, and as Joel Spolsky describes in the wonderful foreword to Coder to Developer, they are convinced they know everything they need to know about software development. The sad fact is that they do not. They are not bad people; they just do not know what they do not know.

There is a bunch of stuff that a developer needs to know that has nothing to do with how a language works. Or how to program, but more how to ship a product. How to balance life and work. What is cute and what is effective. When to invest in new technology and when not to.

In some ways the modern movements to define good processes is making it worse. It used to be new college grads thought they knew everything about writing code, but understood they hadn’t be prepared to run projects and deliver products. Now they have their pet methodologies and are convince they know how to run the project.

Technorati Tags:

WordPress Archive Popup Menu

I’m trying to my the Mrs. blog have the theme she wants with the features she wants. She was using a really ugly one because it let her collapse the things in the side bar.

The first thing I wanted to do and I wanted it for my blog too, was to make the archive by month a popup list.

WordPress has a tag that helps with this wp_get_archives. You can choose to have it output option tags, which are what a select tag needs. After a lot of hunting and Google seaching I found the snippet of code I needed.

<form action="" name="pulldown2">
<select name="mypulldown2"
onchange="document.location=pulldown2.mypulldown2.options[selectedIndex].value">

<option value="">Select</option>
<?php wp_get_archives('type=monthly&format=option'); ?>
</select>
</form>

Now to add it to my blog.

Zero is not nil

I’m learning the Ruby scripting language while learning Ruby on Rails. One of the constants of Ruby is that everything is an object.

I was just coding in Objective C and one of the cool things you can do is call a method on a nil object and it just returns nil. This is nice because you don’t have to go checking for nil every time you get an object before calling a method.

So I got to wondering, in Ruby is the integer object 0 nil? So I wrote a quick Ruby program to see.


if 0.nil?
puts "true"
else
puts "false"
end

This outputs false because 0 is not a nil object. There is an object called nil so


if nil.nil?
puts "true"
else
puts "false"
end

is valid code and it outputs true.

Teach You Kid to Program

Found a great two part blog entry on teaching a 6 year old to program.

I liked part two the best because it contained all kinds of funny nuggets. Firstly, after being a Mac Programmer for 12 years, I was surprised and glad to see this comment:”But the Mac is Unix. It is a hacker’s system.”

Though I almost picked up the phone to call CPS when it was followed by “It comes with all the important languages: C, Java, perl, and python, not to mention bash and sed and awk. And vi. And emacs.” If he makes a 6 year old use vi…. Well he wasn’t totally cruel, only made him use emacs.

Then he taught him Python, which of course will teach him all kinds of bad habits about delimiting code. 🙂

But kids are awesome and I loved what his son thought was important vs. what his programmer Dad thought about.

Now if super-homeschooling Dad would teach his 12 and 13 year old how to program…

Referrer Spam

I’ve been getting spam referrers in my referrer list for the past few days. I already have a list of domains/URLs that don’t make it onto my page of referrers. They are in my log file, but don’t show up on the page when you look at the list.

This list was originally to keep robots out of the list and links to myself. It easily adapted to get rid of these spamers. I looked around the net for a list of spammer URLs that others have run into but didn’t find one. So I’m providing a link to my list. It needs to be clear the first few domains are not spam, they are robots I don’t want in the list. There is nothing wrong with them.

A little more searching found this blog entry on how to get rid of comment spam in MT. He has a list of URLs that he filters out and I added it to the bottom of my list. I had to remove the comments because of the way my script works.

I also added a weird string “search?encquery” to filter out these weird URLs from AOL that look like searches, but have random text in them and no query.

On a related note remember you can see my list of search engines as well. This is the list used in my referrer script to display search results.

Written while listening to “Matrix [trance mix]”
by Rave
Written while listening to “Show Me Love [Extended Version]”
album 200 km/h in the Wrong Lane
by t.A.T.u.
Written while listening to “Your Song”
album Greatest Hits
by Elton John
Written while listening to “Let The Good Times Roll”
album ^a
by Cars

Scripting Test

nice12_thumb.jpg

Using the new WebDav features of DreamHost I uploaded the picture at via AppleScript. The script is a droplet that looks to see if the media directory is there. If it is then it scales the picture using Graphic Converter, and copies the thumbnail and big picture to the folder for the month.

New in this version it checks to see if the thumbnail is already there before creating and scaling it.

First version didn’t put the correct size for the image. Problem is it will only do it if it needs to scale the picture. Need to work on that.