|
Daniel Azuma
|
||
|
Rails/ActiveRecord 2.1 and TEXT/BLOB fields
Posted on Fri, Jun 27, 2008, at 04:52 PM
(0 comments)
We just ran into a rather annoying gotcha when upgrading our application from Rails 2.0.2 to 2.1. All of a sudden, a few the columns in a few of our ActiveRecord classes unexpectedly started defaulting to nil, and refusing to save with a MySQL error. We tracked this down to a change in how ActiveRecord 2.1 is handling default values for BLOB and TEXT columns in a MySQL database. BackgroundActiveRecord uses the database schema to automatically “construct” classes and populate default values. For the most part, this works great. You can create a table such as:
and ActiveRecord will automatically know that the class
Unfortunately, the database doesn’t always cooperate with this mechanism. MySQL (as of version 5.x) does not allow default values to be specified for The old behaviorIn Rails prior to 2.1, ActiveRecord dealt with this by always providing a default value for such types. e.g.
This behavior could be considered a little confusing, but it was the behavior, and it seemed to make sense since What changed, and why is it important?As far as I can tell, what changed in Rails 2.1 is this patch. It removes the implicit default value. Now, the behavior for our
This means the behavior has changed (and caused some whiny-nil exceptions in our code until I fixed them). But perhaps more critically, this fails:
The Is there a solution?I won’t get into the debate here regarding what the Right Thing To Do should have been. For now we needed a fast Rails 2.1 upgrade path, and so we’ve put in a quick-and-dirty solution by going through our ActiveRecord classes that are affected by this issue, and modifying the
One could of course imagine patching this capability into the ActiveRecord DSL, but we didn’t think that was worth it for the few cases we were running into. Is there a better solution? Am I missing something obvious? HTML comments and Rails views
Posted on Sat, Jan 05, 2008, at 01:25 PM
(0 comments)
Comment your code—it’s a mantra they drill into you in Programming 101. I know, most of us took that class at age 5 or 6, but it’s still important 30 years later. Of course, in those bygone days, we were probably programming in C (or maybe Java for you young’uns, or FORTRAN for the graybeards). Comments were there for your benefit as a programmer, and the compiler helpfully threw them away when building binaries for end-user consumption. You could put anything you wanted into your comments and not worry about them becoming public knowledge. Anything, including:
or
or
and only the other developers could see them. Of course, if the girl in the next cubicle was also a developer, it wouldn’t be terribly smart to put such a comment in your code. And that’s what I want to talk about, because this sort of thing seems to be happening more often than it should… The danger of HTML commentsIn this era of high level languages and web development, you have to be careful because not all code is compiled, and sometimes comments end up visible to end users. Comments in HTML are a particular culprit. Anything you put into an html document, including:
is visible to any user who knows how to “view source” in a web browser. I’ve viewed-source on a lot of web sites, and seen a lot of html comments that probably should not have been exposed to the public. At worst, these can be embarrassments, or even security holes. Similar issues surround documents served directly out of your public directory, such as javascripts and stylesheets. Any such files can be found and viewed directly by anyone with a passing knowledge of html. Do you think no one will look at your CSS files? I’ve done so many times, studying how other sites style their pages, and I’m sure I’m not the only one. And you don’t want to know how many times I’ve seen something like this at the top of someone’s publicly-visible CSS file:
You don’t really want me to see your subversion header and gain information about your development infrastructure, do you? Then don’t include it in those files, or write some logic in your production deployment process that strips it out. Better yet, use an optimization/obfuscation tool, especially for javascript. You’ll hide sensitive comments, and make your users happier by reducing load time and bandwidth. A simple solution for Rails viewsWhen writing a Rails view using erb/rhtml, don’t use html comments unless there’s a genuine functional reason it needs to be in the final html document itself. Instead, erb supports a syntax for ruby comments that will not be rendered into the final document:
But sometimes, don’t we want those comments there for development and testing purposes? Perhaps, as I’m debugging a view, I want to visually delineate sections of an html document, or display a value in a comment, but not show (or spend time computing) that value in production. We’ve written a simple method in our
Now, in our rails views, we can do the following:
You can recognize this line as a comment in your .html.erb view file. Furthermore, the comment is rendered into the html document in development, test, and staging environments, but not in production. If a comment requires some computation, we pass in a block instead:
The computation is run and the comment is rendered in development, test, and staging, but the computation will not even execute in production. ConclusionsNo one doubts that ample and strategic commenting is essential in any software development project. But web developers need to be conscious that some of their code can be viewed by an end-user. I don’t know, this all seems obvious to me, but I’m still amazed how many times I’ve seen potentially sensitive comments plainly visible in html. You need to assume that your users are viewing-source, and manage your comments, especially sensitive comments, accordingly. A simple tool like the one above can be a great help. Ruby threads and Kernel#require
Posted on Wed, Dec 12, 2007, at 03:08 PM
(5 comments)
For people like me with a Java background, one of the first things we bemoan and disparage when learning Ruby is its thread support. Simply put, Ruby threads are green threads, meaning they are implemented in user space. Among other things, this means they cannot be scheduled onto multiple processors or cores because the operating system does not know about them. There are other issues, such as the fact that often-used libraries such as ActiveRecord are not thread-safe. This article gives a brief but fairly good overview of the issues, relevant to the current MRI 1.8 implementation. InfoQ also published a very interesting article last spring on the pros and cons of different threading strategies related to Ruby. I recently had to design a multithreaded library for Zoodango’s deployment system, and I ran into another gotcha that I’m sure is known by someone, but I haven’t seen documented yet. Basically, the Let’s consider an example. Suppose, we have a ruby file ”
The As far as I can (experimentally) determine, the check-and-set that controls the “already loaded” flag occurs right at the beginning of the require method. So the following is a potential run of the above code:
You could argue that the above example is contrived. But this takes place in real code too. I ran into it when using Jamis Buck’s excellent library Net::SSH. Net::SSH lazily loads parts of its implementation as they are needed, executing a bunch of I’m still searching for a good general solution for this. (And I haven’t mucked with the ruby trunk so I don’t know if it’s still an issue in 1.9.) For now, what I’ve done is attempt to wrap code that I know could contain dynamic Update (27 June 2008) Net::SSH 2.0 and related libraries do in fact ditch the Needle dependency, so this issue no longer tends to show up when multiple threads access Net::SSH. However, the underlying issue with Kernel#require is still there. The Rails Rdoc Template
Posted on Sat, Dec 08, 2007, at 10:26 PM
(0 comments)
It’s amazing what you find if you just bother to look. I was updating Zoodango’s internal rdocs, and I got thinking, the layout that rdoc generates by default is not how I’d do it. I often have fairly long lists of files, classes, modules, and methods, and the three scrolling lists across the top are too small to scroll effectively. Just try scrolling through the class list on the Ruby core library rdocs and you’ll see what I mean. Most people’s screens and browser windows are horizontal, so I couldn’t understand why the template would take up valuable vertical real estate in that way. The rdocs for Rails look much better—moving the scrolling lists to the left side. Not to mention that the formatting looks quite a bit nicer. Lo and behold, I just discovered, it’s not hard to get the Rails-style view in your rdocs. The Rails rdoc template, it turns out, was written by Jamis Buck, the author of Capistrano, way back in early 2005. The template itself, along with instructions on how to use it, are on this blog posting from back then. Jamis’s instructions say to install the |
Recent
Tags
Random blogs
|
|