This site is dedicated to further knowledge about creating Ruby on Rails applications professionaly. We discuss Ruby on Rails features from a performance angle, discuss Ruby on Rails performance analysis methods, provide information on Ruby on Rails scaling and benchmark Ruby on Rails performance for each release. We discuss best practices for selecting Ruby on Rails session containers, fragment and page caching and optimizing database queries.

New plugin: query_builder

Posted 23 Sep 2006

I’ve added another plugin for you to check out: query_builder.

This plugin enables you to define finder methods that bypass the overhead of construct_finder_sql.

Inside an ActiveRecord model definition,
  define_finder query_name, query_type, options_hash
will create a SQL query method called query_name from a given options_hash. query_type can be :first or :all. The plugin supports all options except :include, but ignores with_scope options. Example:
class Recipe
  define_finder :find_all_of_user, :all,
     :conditions => 'user = :user AND priv < :priv'
  end
This defines a query method which can be called like so:
Recipe.find_all_of_user :user => 'martin', :priv => 1
This call is equivalent to
Recipe.find :all, :conditions =&gt;
            [ 'user = :user AND priv < :priv',
              {:user => 'martin', :priv => 1} ]
If options[:positional] is not nil or false, the created query method will use positional paramaters instead of a hash. In this case, arguments are created in the order of appearance on the parameters passed to define_finder. Therefore
define_finder :find_all_of_user, :all,
      :conditions => 'user = :user AND priv < :priv',
      :positional => true
will create a query method with parameters user and priv, which can be called like so:
Recipes.find_all_of_user('martin', 1)

I converted one of my pages to use this style and it gained 10% more performance.

If you like this plugin, drop me a line. If you find a bug, please submit a bug report to Trac

Enjoy.

Posted in plugins | Tags performance

Comments

blog comments powered by Disqus