# Rquerypad, improve activerecord in association query

**URL:** <https://www.ruby-forum.com/t/rquerypad-improve-activerecord-in-association-query/132135>\
**Category:** Rails\
**Created:** [March 17, 2008, 10:58am UTC](https://www.ruby-forum.com/t/rquerypad-improve-activerecord-in-association-query/132135 "2008-03-17T10:58:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![scorpio\_leon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@scorpio\_leon](https://www.ruby-forum.com/u/scorpio_leon)\
**Post date:** [March 17, 2008, 10:58am UTC](https://www.ruby-forum.com/t/rquerypad-improve-activerecord-in-association-query/132135/1 "2008-03-17T10:58:47Z")

</div>

[http://code.google.com/p/rquerypad/](http://code.google.com/p/rquerypad/)

Simplify query options with association automation and improve inner  
join for activerecord of rails

# Feature

1. single name string decribe associations
2. support to mix inner join and outer join with any order
3. auto remove duplicated joins from other association in different  
depth
4. support :conditions, :order, :group
5. auto merge to original :include, :joins

# Install

ruby script/plugin install  
[http://rquerypad.googlecode.com/svn/trunk/rquerypad](http://rquerypad.googlecode.com/svn/trunk/rquerypad)

# Example

suppose the asscociations of User ↔ Thread ↔ Reply is 1:N:N

@users = User.find(:all, :group =\> [“threads.created\_at”, “name”])  
generate:  
[:all, {:group=\>“threads.created\_at, users.name”, :include=\>[:threads]}]

@users = User.find(:all, :conditions =\> [“threads\_.replies.title = ?”,  
“rquerypad”])  
generate:  
[:all, {:inner\_joins=\>[“threads”], :conditions=\>[“replies.title = ?”,  
“rquerypad”], :include=\>[{:threads=\>:replies}]}]  
#note: the :inner\_joints is processed by rquerypad before sending sql to  
database

@users = User.find(:all, :conditions =\> [“threads.replies.title = ? and  
threads.id = ?”, “rquerypad”, 1])  
generate:  
[:all, {:conditions=\>[“replies.title = ? and threads.id = ?”,  
“rquerypad”, 1], :include=\>[{:threads=\>:replies}]}]  
#note: single “threads” was removed from includes

# Usage

put the following code in you initializer

#if you want to print log, use Rquerypad.prepare\_with\_debug  
Rquerypad.prepare

# Test

1.Prepare

the test depends on sqlite3 database, the following code should be add  
into your database.yml and place rquerypad.rb(copy from test.rb) in  
config/environment

rquerypad:  
adapter: sqlite3  
database: vendor/plugins/rquerypad/test/db.rquerypad  
timeout: 5000

2.database migrate

execute the following script

rake migrate

3.start test

execute the following script  
rake
