Archive for January, 2009

what a night….

January 31st, 2009  |  Published in Uncategorized

I’ve kept personal accounts out of this blog for most part, until today. It was late and we were heading home from visiting some friends of ours. We pulled into our apartment complex and saw an suv parked on the side of the road and someone lying on the ground in the middle of the road. We thought they got hit but as we got closer we realized that this woman, lying on the cold ground, was beaten severely and thrown out of a car that fled the scene. She was conscious but clearly in a lot of pain, and there was more blood than I could stomach. We put a blanket on her and waited for the police and medics to arrive. I have never felt so helpless in my life.

They arrived at the scene and questioned her about what had happened. All we could hear was that the man who did this to her was the father of her two children, and he took her car and her children with him after throwing her out. She kept asking for her children and they were no where in sight. They took her away in an ambulance fairly quickly.

We see these things all the time on television and in movies but it never hits you like this. Never do you feel so much compassion and fear and despair and hopelessness and helplessness and anger… the list is so long. I doubt I’ll sleep much tonight. Her face is engraved in my mind. I can’t express how little faith I have in the world right now…

Safari list items mad cropping on line wrap

January 15th, 2009  |  Published in Uncategorized

I’m sitting at work today and messing around with list li items so that I can display them inline and they wrap nicely. Definitely not rocket science except that when I’m done and I check how it looks in Safari, I get something that looks like the following:

Safari not ok

Seriously Safari? Why do you crop my list items when they wrap to the next line??
The structure of the above blue elements is as follows:

ui
  li
    a
  li
    a
  ...

Well it turns out that the issue was as follows:
Broken CSS:


li {
  display:inline !important; <= BAD!
  margin-left: 5px;
  list-style: none;
}

Good CSS:


li {
  margin-left: 5px;
  list-style: none;
  display: block; <= GOOD
  float: left;    <= GOOD
}

Really Safari? I did not enjoy that.

Rails model callbacks and associations order

January 15th, 2009  |  Published in Uncategorized

I learned one of the most important rails rules yesterday through sheer luck and Google power. I had a before_destroy callback on one of my models and one of the instructions inside the callback just kept failing because an important resource related to the instruction was already destroyed.
Here’s a very trivial example. Lets say I have a person who has many dogs and each has a breed. Breed keeps track of how many dogs of that breed exist using a simple counter in its table.

class Breed < ActiveRecord::Base
  ...
class Dog < ActiveRecord::Base
  has_one :breed
  ...
class Person < ActiveRecord::Base
  # Associations
  has_many :dogs, :foreign_key => 'dog_id', :dependent => :destroy

  before_destroy { |person|
     # Get the breed count and reduce the number of dogs by one
     person.dogs.each do |dog|
         dog.breed.decrement_dog_count
     end
  }
  ...

And this results in….. epic failure. Why? Because first a person’s dogs will be deleted. THEN the before destroy callback will be invoked. Silly isn’t it? And tough to catch because the reason is their order of listing. If we write it as follows:


class Person < ActiveRecord::Base
  before_destroy { |person|
     # Get the breed count and reduce the number of dogs by one
     person.dogs.each do |dog|
         dog.breed.decrement_dog_count
     end
  }

  # Associations
  has_many :dogs, :foreign_key => 'dog_id', :dependent => :destroy
...

… all is well in pet land.