Thursday, 17 May 2007

ActiveRecord :select with :include

ActiveRecord is an important slice of the joy included in Ruby on Rails, writing a SQL database driven application without actually writing any SQL is really pleasurable.

But sometime, even in the joy you get some pain.
The pain for me arrived when I was working on some reporting feature that was pulling various data across a quite long chain of has_many relationships.

I experienced a quite high load in the DB at the moment of the query and after that seconds of CPU full load eaten by my ruby process.
I checked what was going on in my log/development.log and I noticed the query was extracting all the columns of every table even if I specified a :select option, this turned a query originally quite light into a heavy one.

I googled a bit and I found out that with ActiveRecord, if you specify :select and :include option you don't get anymore 100% joy but just 50%, your :select statement will be ignored.

Today I'm trying to put back some percent of the joy.

select_with_include gem is meant to move the joy level up to 80% (no, not 100% yet)

select_with_include will allow you to specify a limited :select statement like


"table1.column1, table1.column3, table2.column2"


or like


"table1.*, table2.column3, table2.column4, talbe3.*"


At the moment you can't specify functions or calculated fields. There are reason for that and I'm going to discuss these reason in the next posts. I'll try as well to explain how this gem works around the original ActiveRecord code.

For the moment you're invited to get select_with_include gem using


sudo gem install select_with_include


and test it yourself specifying


require 'select_with_include'


in your config/environment.rb file.

If you're a script/console user you can just try, while you

tail -f log/development.log

to launch the same find with :include and :select with and without requiring the gem.

If you find any issue you're really welcome to use the issue tracker


And if you want to know more just stay tuned for the next posts

Wednesday, 16 May 2007

attachment_fu and Rmagick FAT thumbnails

I'm using attachment_fu on the project I'm currently working on, using it to process the images the users upload.

As backend I use Rmagick, since it was already used in the same app. At some point I felt curious about the size in kb of the thumbnails generated, to estimate the increase of bandwidth the new feature would add on the server.

I quickly spotted the size of the generated jpegs was unreasonable for the pixel size of them.

It turned out that attachment_fu + Rmagick on Ubuntu 6.0.6 and Ruby 1.4 aren't compressing jpegs.

To fix here's a one line patch on attachment_fu.

this patch sets the jpeg quality to 85%


[~]$ cat atch_fu/attachment_fu_rmagick_compression.patch
Index: lib/technoweenie/attachment_fu/processors/rmagick_processor.rb
===================================================================
--- lib/technoweenie/attachment_fu/processors/rmagick_processor.rb (revision 2864)
+++ lib/technoweenie/attachment_fu/processors/rmagick_processor.rb (working copy)
@@ -45,9 +45,9 @@
else
img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols, rows) }
end
- self.temp_path = write_to_temp_file(img.to_blob)
+ self.temp_path = write_to_temp_file(img.to_blob {self.quality = 85})
end
end
end
end
-end
\ No newline at end of file
+end