Sunday, September 28, 2008

Compiling Sphinx in Ubuntu

I had just watched Ryan Bates' screencast on Sphinx and I wanted to install it on Ubuntu. Took me a while to find out how, so here are my instructions. I'm on Ubuntu 8.04

1. Download the latest realease. It's 0.9.8 as of writing.
2. Install it with their instructions. I ran into a problem though - I have MySQL already but not the files Sphinx needs to install. If you ran into this problem, continue:
3. In console, type:

mysql_config --cflags

That will tell you the package you need to install.
4. Mine was libmysqlclient15-dev:

sudo apt-get install libmysqlclient15-dev

5. Try installing Sphinx again, it should work this time.

Tuesday, September 9, 2008

attachment_fu, Rails 2.1 and before_thumbnail_saved

I was working on before_thumbnail_saved callback in Rails 2.1 and it seems it has changed! It didn't work with me anymore. At least my old code didn't.

Apparently, you only pass the thumbnail into the loop.

Read this blog post for details.

Friday, September 5, 2008

Rake: Update or create images generated by attachment_fu

Did you upload a bunch of images and find out that the new layout needs a new size of the photos, and you didn't specify this in the has_attachment line of your model?

This happened to me, and I wanted an easy to way update all images in the database. I didn't test this with millions of records or anything, but if you want to fix this up please leave a comment.

Notes:
  • To use, simply edit has_attachment line with the new thumbnail sizes you want.
  • IdeaImage is the model that I want to update
  • parentless is a named_scope that gets only the IdeaImages whose parent_id's are null.

namespace :images do
desc "Updates all the thumbnails of idea_images based on the thumbnails hash in the model"
task(:update_idea_images => :environment) do
IdeaImage.parentless.each do |i|
#destroy the thumbnails first
i.thumbnails.each { |thumbnail| thumbnail.destroy } if i.thumbnailable?
#then recreate the thumbnails
i.attachment_options[:thumbnails].each { |suffix, size| i.create_or_update_thumbnail(i.create_temp_file, suffix, *size) }
end
end
end