Rails

Stories from the trenches of my day job building Web applications.
  • 23

    APR
    2008

    The Nice New Italian Restaurant of Server Monitoring

    David Heinemeier Hanson, the creator of Rails, has made one of his business strategy talks available recently. This is a great talk about how we all might be trying just a little too hard at what can be a fairly simple task.

    We all hear advice like this over and over again, but we seem to forget it so fast that we need that constant reminding. Just today I saw a site redesigned to improve a section users haven't even seen yet (as that section isn't yet public). Is that really the top use of resources for an unlaunched portion of the site? How do we know they wouldn't have liked it? How do we know they will like the new version better? I'm sure it won't surprise readers to learn that this site is over budget on time and money.

    The point of all of this is that I want to tell you about a new server monitoring tool the company I work for has recently launched. The new service is called Scout and we've done our dead-level best to keep to the simplicity principal both because we agree with David and because we flat out need it to work that way.

    Read more…

    In: Rails | Tags: Monitoring | 2 Comments
  • 10

    APR
    2008

    Five ActiveRecord Tips

    This article was written for the Railcasts 100th Episode Contest. I think the idea is great and I look forward to reading great tips from all who decide to participate.

    1. create_or_find_by_…

    I imagine most of you know that ActiveRecord can handle finders like:

    MyARClass.find_or_create_by_name(some_name)
    

    This will attempt to find the object that has some_name in its name field or, if the find fails, a new object will be created with that name. It's important to note that the order is exactly as I just listed it: find then create. Here are the relevant lines from the current Rails source showing the process:

    record = find_initial(options)
    
    if record.nil?
      record = self.new { |r| r.send(:attributes=, attributes, guard_protected_attributes) }
      #{'yield(record) if block_given?'}
      #{'record.save' if instantiator == :create}
      record
    else
      record
    end
    

    The above code is inside a String literal fed to class_eval(), which is why you see interpolation being used.

    Unfortunately, this process is subject to race conditions because the object could be created by another process (or Thread) between the find and the creation. If that happens, you are likely to run into another hardship in that calls to create() fail quietly (returning the unsaved object). These are some pretty rare happenings for sure, but they can be avoided under certain conditions.

    Read more…

  • 15

    JAN
    2006

    What Not to Test

    I've now seen multiple claims amounting to something like, "I built this Rails application with just testing." I think it's great that people can do that. They are obviously smarter than me. I need a browser to build a Web application.

    I do test my Rails projects, of course. I'm a huge fan of testing and I'm always telling people how much it could help with their work. However, I believe there's a time to test and a time not to, if you can imagine that. Yes, you heard me right, there are things I don't test and I'm sure this next revelation will shock you even more:

    I don't test Rails views.

    Now before everyone fires up their mail client and pours on the hate mail, let's make sure we are very clear about what I just said. I do write thorough unit tests for all of my model classes, of course. I also test the controllers as much as possible. I make sure the data I expect ends up in the right instance variables with the magic assigns(). I also use the session Hash to validate what I am remembering about the user. If a controller action is a complex logic branch that can end up in several different places, I will also make sure I add some assertions to verify that the right template handled the response.

    Read more…

  • 4

    JAN
    2006

    Add to Subversion

    I love Rails generators and I love Subversion, but I'm not so sure they love each other.

    Little is worse than generating some scaffold files or a login system, tediously typing svn add ... a bunch of times, and later learning that you missed a file. Oops.

    Given that, here's my hack to get the Rails generators and Subversion to kiss and make up:

    desc "Add generated files to Subversion"
    task :add_to_svn do
      sh %Q{svn status | ruby -nae 'system "svn add \\#{$F[1]}" if $F[0] == "?"'}
    end
    

    I put that in "lib/tasks/add_to_svn.rake" for all my Rails projects. You can then access it whenever you need with rake add_to_svn.

    I've seen similar tasks posted, but they've always been Unix specific and I work with some Windows programmers. This is my attempt to help both on platforms.

    How does it work?

    You're probably not use to seeing ugly Ruby code like that, but Ruby supports a lot of command-line shortcuts that it inherited from Perl. Here's a breakdown of the options used:

    -e The code to execute directly follows this option.
    -n Wrap my code in a standard Unix filter style processing loop.
    -a Auto-split each line of input.
    

    Read more…