Thinking Sphinx is a fantastic plugin for Ruby on Rails that makes using Sphinx pretty much a point-and-drool experience.

But sometimes it may be tricky or inconvenient to perform some operations using Sphinx, and you want to mix in some regular SQL. Here’s a very easy way to combine Thinking Sphinx searches with named scope:

  named_scope :sphinx, lambda {|*args| {
    :conditions => { :id => search_for_ids(*args) }
  }}

Ordering ActiveRecord result sets by RANDOM() with Sqlite3 can be very handy. For example, here’s a named scope that would order records randomly and limit the result set to 5 results:

named_scope :random_five, :order => "RANDOM()", :limit => 5

The only problem with this is, if you’re using SQLite for testing and local development, and MySQL for deployment, your code is going to break because MySQL does not use the same random syntax that SQLite does. You can’t entirely blame the frequently-standards-incompliant MySQL here though, because ordering by random isn’t a part of standard SQL and is added as a stored procedure or vendor extension in most RDBMS’s.

To solve this problem, I’ve created a small monkey-patch to Rails which allows you to use code like the following:

named_scope :random_five, :order => :random, :limit => 5

You can access the code here.

What do you think? If you find this useful, let me know and I’ll submit it as a patch to Rails.

A pastie is worth a thousand words:

http://pastie.org/249500

It’s not pretty, but lets you create a simple in place editor that works with RESTful controllers. Ideally, this should be done unobtrusively, but if you need to pass in a lot of options (for example, to change language used in the controls), then doing this unobtrusive can become a bit of a PITA.

The only “weird” thing you need to do is make your show method have a block that responds to requests for JSON if you want to use the loadTextURL option.

I’ll see about doing this up as a simple plugin if anyone is interested.

If you want to see a nice rails plugin that does some of this in a more unobtrusive manner, check out Pat Nakajima’s better_edit_in_place plugin. It’s still being actively developed but looks like it will soon be an excellent resource.

I’ve been working with Git for the last few weeks and have decided to move friendly_id over to Github. The svn repository will remain live for at least a month of so after Rails 2.1 is released, since the support for git-hosted plugins is currently only available in Edge Rails.

For now, all development is being committed to Git, and then patched into the svn repository. So, if you’re using the svn repository don’t worry, it will remain current for a while.

I remember first hearing about Git during the Linux/Bitkeeper contoversy, and recall thinking Linus Torvalds was odd for not using CVS or Subversion over Bitkeeper, and then thinking he was certifiably insane for deciding to roll his own version tracking system once Bitkeeper didn’t work out.

Of course, Linus Torvalds is (a) a better programmer than me and (b) smarter than me, so there you go. Git has evolved into a very nice tracking system; for me everything that was slow and annoying in SVN (namely, comparing revisions and branching) is fast and easy using Git. If you haven’t yet taken a look at it, you should.

You can take a peek at the shiny new friendly_id repository here.

If you’d like to see a quick introduction to Git, take a peek at Sebastian Delmont’s presentation below.


GIT Talk (nyc.rb) from Sebastian Delmont on Vimeo.

I got stuck for a few minutes trying to figure out how to write a unit test to check if a value was being set in the flash using flash.now. In Rails’ Test::Unit you can’t just do something like assert_not_nil flash[:notice] because the flash value is discarded at the end of the request, and so the flash is always nil.

The solution is to use Mocha to mock an instance of ActionController::Flash::FlashHash and capture the value being set. Here was my solution:

def test_create_with_empty_email_sets_error_in_flash
  hash = {}
  ActionController::Flash::FlashHash.any_instance.expects(:now).returns(
    hash
  )
  post :create
  assert_not_nil hash[:error]
end