TJ Singleton Developer and Preacher

26Aug/093

Sometimes programming problems are best solved AFK

This afternoon I ran into a problem. I was working on adding in some new form fields on an edit screen. The fields were for an associated model. I was using fields_for in the view and accepts_nested_attributes_for in the model. Everything looked great, but my specs were still failing. The form just wasn't updating the association.

I desk checked the code. I tried it in script/console. I checked the controller logs. I tried everything I could think of. I struggled for an hour or so before I called in for help. By then end of the day, neither a more experienced developer or myself could figure out why it wasn't working. We were ready to blame plugins, rails, or anything else. Looking over the code, everything looked correct.

Calling the @my_model.my_association_attributes= worked, but the controller was never calling it! Can you guess what the culprit was? The model had attr_accessible set!

So is the moral of the story to add the association_attributes on models with attr_accessible? Not exactly, see I didn't solve the problem at my desk. I solved the problem on the way home from church about two and a half hours later. I wasn't even thinking about the problem when it popped in my head.

Sometimes the right course of action is to just step away from the problem for awhile, and let our mind work on it. A week or so ago, I had struggled for a couple hours trying to figure out how to solve a particular problem cleanly. I had about half a dozen ways to solve it, but thinking of actually implementing any of them made me feel dirty. I knew there was a good way to solve this problem.

How did I come up with a solution? I slept. When I woke up, brushed my teeth, and fired up textmate the next morning in my head was a elegant (and obvious) solution to the problem at hand. Sometimes our minds just need a break.

Next time you get frustrated, step away for a bit. Let your mind relax and refresh. AFK isn't as bad as it sounds, and sometimes it's medicinal.

Filed under: Ruby Leave a comment
Comments (3) Trackbacks (0)
  1. Great post! I have done some of my best coding while doing something else. Maybe it’s a sign that we should take more breaks from the keyboard.

  2. Absolutely. No matter how much you want to sit there until it’s figured out, sometimes the best course of action is to do something completely unrelated. I have a few non-computer related things I do that seem to get my brain working right again, exercise being one good one.

  3. Thx guys!

    Coincidentally, I just made it to this post in my read it later queue. http://lifehacker.com/5313756/a-wandering-mind-can-be-hard-at-work


Leave a comment


No trackbacks yet.