Rails: The Beginning
I finished up the Rails books this weekend and finally started coding up my first project. The project is a secret now not because it’s great but just because I like keeping secrets, it’s what gives my blog that special sauce that you all can’t get enough of. Or I don’t want to be embarrassed if it doesn’t work out and I don’t ever show anyone. One or the other.
The book itself I found ultimately disappointing. Not as a commentary on Rails but there was just something not overly good about the book. It was fine, but there were too many times where I felt confused or puzzled and I thought the editing was inconsistent.
I want to chat a little bit about my first impressions of working in Ruby and Rails. These are just first impressions and any issues I have could (and hopefully will be) non-issues.
Installing Ruby and Rails was pretty quick and painless. I haven’t tried installing the apache module yet, just working with WEBrick so far, but it went fine got my database all connected and was ready to go. It is pretty easy to get going and the scripts that help you generate all the stub files you need to get going are really convenient. I really like the migration system, it does a bangup job of things and I can see how if you’re maintaining a reference application that needs to roll out to other servers, it’ll be really handy.
In general I like the way MVC is enforced but I am having some problems wrapping my head around how certain things should be done. My problem with things of this nature (frameworks) are that they try to do a lot of stuff for you, the more they do for you the more invasive it is and the more aspects of processing get touched in the process. Each of these touches is an assumption that you will be doing things in the framework way, the more assumptions they make… well you know what happens when you assume, right?
Rails makes a valiant effort to have it’s cake and eat it too. It makes a lot of assumptions but tries to allow you to circumvent those and do things in your own way. Sadly, it’s so complex and does so much for you that I don’ t think it can get out of your way. I’m only 2 days into development (and not even that many hours) and I’ve run into two issues that the books and online searching couldn’t learn me how to get out of.
The first is their cool routing table, it’s basically a map of url patterns that tell Rails how to parse and process a url. It’s really interesting and seems quite useful, sadly I have to pass a floating point number in my url’s. Ruby, unfortunately, uses ‘/’ and ‘.’ as dividers in the url - so I can’t simply parse the floating point number in the url, I have to grab it in two sections (before the decimal and after) and then reassemble it in my application. If I needed access to this in a bunch of actions, I could probably use a filter, but it is annoying that I can’t just parse it somehow in the routing.
Second and much more problematic is that apparently somewhere deep in the bowels of ActiveRecord it uses % substitutions for its own purposes. Unfortunately, % substitutions are used in SQL - like in the date_format function - so when I try to use date_format it errors out. They provide another way to access sql, but that doesn’t accept bind params, so doesn’t really work out for me. I’ve sent an email into the Ruby list to see if there’s something else I’m not seeing and I guess I’ll try and investigate how to work directly with DBI. (UPDATE: I figured this out, you have to send the format string as a bind variable, which seemed wierd to me but that’s probably just perspective and I can live with it)
More broadly I’m not sure how to organize everything. It seems like Rails wants you to be fairly granular with your controllers - unfortunately you can only have one active controller at a time controlling a view. But what if your view requires the efforts of other controllers? To me this seems like quite a likely scenario for any moderatly complex site - you can imaging in a content site, you might have an article controller, a user controller, a blog controller and an e-commerce controller. In your layout you might show elements from all these controllers on every page - how do all these variables get set up? According to the Rails book you would have to use before filters on every action in every controller to call all the methods to set up all the variables you needed. It becomes even more annoying if not every action shows every piece or an action conditionally shows certain pieces, you end up with a lot of coding or else setting up variables that are never used.
They rule out components, the bundling of a template and a controller action as slow and inefficient. I don’t know anything about components and don’t want to use a deprecated feature, but it seems to me to be a mistake. In my view, if you have a template (or partial template) that requires some variables there should be a way in that template to call back to whatever method needs to control it. Maybe there is, by calling a class method at the beginning of the template to do the processing. I suppose rails frowns on such breaking of the MVC rules, though. I’m still trying to do everything the Rails way to learn all the benefits that they propopse I’ll reap, but I remain skeptical about dogged adherence to a theory. We’ll see.
Lastly I don’t love the documentation. Being used to perl and perldoc - I find Ruby’s documentation lacking. The book seems to punt on a lot of documentation, going lite on description and telling you to look online, except online doesn’t seem all that much better. And making you start a webserver to read the provided documentation is just crazy.
I’m still pretty into this, I’m hoping most if not all of my issues are just because I don’t know better and over time I will become smarter and faster. I was able to do a remarkable amount in relatively little time, given that I don’t know the language or the framework and that is really promising to me. Hopefully this post will be a historical record so that a year from now when I’ve guzzled all the RoR kool-aid, licked the pitcher dry and mastered the (ruby) universe I can look back at my paltry problems and have myself a good chuckle.







