Java thinking in a Ruby world

Occasionally I find myself thinking with my Java hat on. Today was one such day. Looking at a legacy Order model from a Java system that we’re rebuilding in RoR, I noticed that it had two status columns – perfect for acts_as_state_machine! Or so I thought. But acts_as_state_machine isn’t really made to track multiple states on one object, and so I found myself with a little problem. How was I going to make this work?

Once I’d snapped out of thinking in the Java mould, it was obvious. These status fields could be broken out into separate models (with other relevant information, of course), each of which uses acts_as_state_machine. Thus I use the association mechanisms of Rails to help me, and work can keep trundling on nicely.

Then later this evening, reading the transcript for a Bruce Tate interview on the Ruby on Rails Podcast, I came across this relevant quote – describing similar thought processes:

I talked about the object-relational mappers that Java has, and all the inheritance modeling that you can do on the Java side, and trying to shoehorn my Java thought process into the Rails model. That took me to a sad place [smiling] and we don’t want to be in a sad place, we want to go to the happy place. So I learned to stretch the way that I think about various problems, like, for example, instead of trying to build a whole inheritance model, sometimes I’ll use polymorphic associations. So instead of saying: "OK, everything on my website is content,” well, if I were to follow the Rails inheritance model, then everything would be crammed into a content table, and all the sub-classes of that would be with different properties. That’s clearly not a manageable solution. One of the things that I can do, though, is break out a small content descriptor that I can then add to a class with polymorphic associations.