Interesting Things

  • has_many and belongs_to associations can now automatically create back references each other, thanks to a Backport of :inverse_of from Rails 3 to rails 2.3.6. This allows us to keep our object graphs more correct and avoid situations where we have 2 copies of the same object because the object graph is walked in reverse. Here's how to use it:
class Parent < ActiveRecord::Base
  has_one :child, :inverse_of => :parent
  accepts_nested_attributes_for :child
end

class Child < ActiveRecord::Base
  belongs_to :parent
  validates_presence_of :parent
end

Danny BurkesDanny Burkes
Standup 5/27/2010
edit Posted by Danny Burkes on Thursday May 27, 2010 at 12:52PM

Ask for Help

Recurring jobs

A pivot asked "what is the current state of the art in scheduling recurring processes?"

The first-order answer was simply "cron", but then the conversation got interesting.

Cron has a few downsides-

  • Each task execution has to re-load the entire ruby/rails runtime, so, you pay a significant penalty in terms of startup time
  • Crontabs often don't get checked into source control, so there ends up being little visibility into which jobs are running when

One suggestion to solve the visibility problem was to use the whatever gem, which allows you to express your cron schedules in a ruby DSL that can easily be kept in source control.

A suggested alternative that eliminates cron altogether is resque-scheduler with resque.

The upsides with resque-based scheduling are that all your schedule logic is expressed in ruby, and you don't pay the ruby/rails startup penalty for each worker.

The downside is that it adds additional operational infrastructure for you to manage (the resque workers and the redis server(s)).

Danny BurkesDanny Burkes
Standup 5/25/2010
edit Posted by Danny Burkes on Tuesday May 25, 2010 at 12:46PM

Interesting Things

  • Rails 2.3.8 is out, fixing HAML compatibility and no longer inadvertently requiring rails_xss.

  • RailsBridge Open Workshop Project is hosting Mighty Ruby Tuesday tonight in San Francisco. RailsBridge puts on workshops around San Francisco (and last week, in New York) that give women a safe place to learn programming, or if they're already programmers, learn Ruby. Pivotal is a sponsor and we support this important effort to bring gender parity to the Rails community.

Danny BurkesDanny Burkes
Standup 5/24/2010
edit Posted by Danny Burkes on Monday May 24, 2010 at 03:40PM

Ask for Help

Identify and uploaded files

Running identify on uploaded files sometimes fails, because by the time identify gets hold of the file, the file extension has been lost (it has been written to /tmp or the like).

The suggested workaround was to see if the filename specified in the Content-Disposition header, if present, could be carried through to the temporary file.

Interesting Things

  • Amazon has recently introduced a reduced-durabilty pricing tier for S3 storage, with significant discounts available to those who can live with 4 9's versus 11 9's of reliability. This seems like a win for things like thumbnails, which can easily be re-created from the originals on the off chance that they were actually lost by S3.

  • Rails 2.3.7 is out, but it doesn't play nice with HAML just yet. Projects using HAML should avoid upgrading until those incompatibilities are ironed out.

Interesting Things

  • One of our clients had a large production issue due to a long-standing bug in Rails with case sensitivity.

Here's the situation: Rails validates_uniqueness_of has a flag called :case_sensitive. This flag defaults to 'true', but can be flipped.

MySQL's default collation is case-insensitive. As a result, queries will, in general, ignore case unless specifically overridden.

So one might imagine that setting :case_sensitive to false would be completely harmless in a standard MySQL application.

One would be wrong. Setting case_sensitive to false changes the query to lowercase the field in question, causing the MySQL database to ignore any indices it may have and turning the validates_uniqueness_of operation from something cheap and quick to something requiring a full table scan.

The open Lighthouse ticket on this issue is: https://rails.lighthouseapp.com/projects/8994/tickets/2503-validates_uniqueness_of-is-horribly-inefficient-in-mysql

Nathan WilmesNathan Wilmes
Standup 5/20/2010: More about SEO routes
edit Posted by Nathan Wilmes on Thursday May 20, 2010 at 09:13AM

Ask for Help

"Any clever ways to catch out of bounds exceptions from Solr?"

This is a follow-up to yesterday's Solr question. After some investigation, it looks like none of the major providers catch out of bound exceptions for very large numbers. Rather than instrumenting every Ruby call with validations to prevent these numbers from getting into Solr, are there any other brilliant ideas?

Interesting Things

  • Follow-up to the help from 5/19/2010's SEO routing question. The latest hotness appears to be FriendlyId (http://github.com/norman/friendly_id) This plugin makes human-friendly slugs and comes with a variety of interesting features, including versioning and slug scoping.

  • Power RubyMine commands:

Goto File + line #: If you use ctrl-shift-N to go to a file, try typing in a line number after a colon, something like "my_file:30". You'll end up on that line.

Analyze stack trace: This tool lets you paste in an external stack trace, and gives you the ability to browse to all of the pieces of that stack trace.

Sean BeckettSean Beckett
Rails devs in Portland, OR: looking for a Revelation?
edit Posted by Sean Beckett on Wednesday May 19, 2010 at 05:24PM

At Pivotal Labs, one of the services we provide is helping clients interview and hire. Pivotal Labs and our clients place a strong emphasis on Agile development and its many aspects: test-driven development, rapid iterations, frequent refactoring, etc.

Here is a job posting from Revelation, a former Pivotal client looking for a lead Rails developer.

Rails Developer - Portland, OR

Revelation creates tools that help people understand other people. Our offerings are used for market research - helping companies better understand their customers to make better things. Our customers include some of the world's leading research and consumer companies. Our culture is one that encourages the intellectually curious and intensely creative.

Are you entrepreneurial in spirit? Do you have the desire to not only improve product, but improve process and tools? Are you passionate about test-driven and agile development? And most important of all, are you ready to make the leap from skilled developer to leading a rails development team?

Come add your experience and thought-leadership to an incredible team. We are looking for someone with great code and people skills, who wants to take on a leading role in the development of our product and our practices. Reporting to the CTO, this position will be responsible not only for developing a great product, but also helping to create a solid foundation for an expanding team.

Nathan WilmesNathan Wilmes
Standups 5/18/2010 and 5/19/2010: SEO routes
edit Posted by Nathan Wilmes on Wednesday May 19, 2010 at 09:19AM

Ask for Help

"Our site requires crafting URLs in a very particular SEO-friendly way. Rails doesn't seem to give us a good solution for our URLs. Any suggestions?"

One of our clients needs to make their app accept and generate compound URLs that look something like the following:

http://my.site/jkrowling-series-harrypotter-book-1

where author, series, and book are all different domain concepts. Rails RESTful resources don't really support this format. There wasn't an immediate solution, but among the peanut gallery of ideas:

  • Hyphens are better than slashes in URL crafting, but Rails doesn't separate on slashes at all

  • to_param solutions - Overriding to_param to something that starts with an integer ID generates URLs that look very slug-like, but can use standard Rails Domain.find mechanisms. For example, a book.to_param might be overridden to be "1-bookname", which works for all purposes. The problem with this solution is that it doesn't quite fit the requirements here, and doesn't cover the compound needs.

  • Custom routes are always a possibility. You can hook up a special (non-resource) controller that understands flexible browse-y routes like the one above, parses them, and delegates to the more standard resource controllers. The problem here is that you have to figure out a decent delegate pattern and route generation pattern.

  • In general, URL crafting is a separate art from domain model crafting, and Rails doesn't really cater to this. You will have to design URL-centric code to suit your URL crafting.

"Any ideas on ways to performance test IE7?"

No immediate ideas, but potentially more later.

"When users enter very large search parameters for numbers we get the following exception out of RSolr:"

RSolr::RequestError: Solr Response: For_input_string_11111111111111111111__javalangNumberFormatException

Is there an elegant solution to this aside from validating that all input parameters aren't larger than max int?

Interesting Things

  • When using named scope methods that refer to other named scope methods, you may discover that your SQL has some redundant condition clauses. This is a bug in Rails 2, and has been true for several versions. However, it's a harmless bug - MySQL will understand the extraneous condition clauses just fine, without performance implications.

  • Mocking Paperclip for tests is a careful art. See our other blog post: Stubbing out Paperclip ImageMagick in Tests

Rob OlsonRob Olson
Stubbing out Paperclip/ImageMagick in Tests
edit Posted by Rob Olson on Tuesday May 18, 2010 at 11:14PM

Many people use the ultra popular Paperclip library to handle file attachments in Rails. Unfortunately the Paperclip documentation does not cover how to stub out calls to ImageMagick in your test suite. Without the proper stubs in place a test suite that uses Paperclip will take much, much longer to run.

In the grease your suite presentation by Nick Gauthier it has a slide titled Quickerclip that describes what needs to be done to spend up Paperclip in tests, basically you need to keep it from shelling out to ImageMagick. Alas, the presentation does include code for how to achieve Quickerclip.

As the presentation shows Paperclip.run is the method that needs to be changed. The first parameter passed to Paperclip.run is the ImageMagick command be executed. Paperclip uses the identify and convert commands. The identify command is used to determine the dimensions of an image. The convert command is the really heavy one that does image manipulation and thumbnail generation. Here is a redefinition of Paperclip.run with sensible behavior for tests.

module Paperclip
  def self.run cmd, params = "", expected_outcodes = 0
    case cmd
    when "identify"
      return "100x100"
    when "convert"
      return
    else
      super
    end
  end
end

class Paperclip::Attachment
  def post_process
  end
end

Redefining post_process in Paperclip::Attachment is an optional additional optimization. In Paperclip, post_process eventually calls Paperclip.run("convert") and by short-circuiting the method earlier in the chain we save a few cycles.

Davis W. FrankDavis W. Frank
Standup 5/17/2010: Nonce-sense Edition
edit Posted by Davis W. Frank on Monday May 17, 2010 at 01:20PM

Interesting Things

  • Keep your OAuth Nonce values simple.

The Twitter Api, which is requiring all clients to move to OAuth for authentication by June 30, 2010, like all OAuth systems requires a nonce value for every call. This value is supposed to be random and unique for each request you make.

While there are many ways to generate a random ASCII value, our recent experience with Twitter's OAuth system shows that a nonce value should not include a '%' character - which would happen if your value has any non-URL-safe character. Twitter will return you a 401 error and tell you that your signature and token cannot be verified.

We've filed a bug with Twitter. But until then, keep your nonce value to ASCII letters & numbers and the calls will work just fine.

Other articles: