ActiveRecord Nested Conditions Improvements Suggestion

I’m not satisfied with how rails currently handles finds accross
active record models, and want to see if there’s a waywe can improve
it.

The ability to generate dynamic search criteria from forms that search
accross models can be quite difficult except in the simplest cases.
The main cause of this is because as a developer you have to work out
what the table name is going to be called before you are able to use
it.

Here is an example:

class Person < ActiveRecord::Base

Everybody has a home address

belongs_to :home_address,
:class_name => ‘Address’,
:foreign_key => ‘home_address_id’

Only some people have a work address

belongs_to :work_address,
:class_name => ‘Address’,
:foreign_key => ‘work_address_id’
end

class Address < ActiveRecord::Base
end

Lets say we have a search panel, that allows you to filter on home
address street name, and work address street name and the user may or
may not use these search options.

A search for the home address 22

Person.find(:all, :joins =>
[:home_address, :work_address], :conditions => {:address =>
{:street_no => 22}})

A search for the work address 22

Person.find(:all, :joins =>
[:work_address, :home_address], :conditions => {:address =>
{:street_no => 22}})
#Notice that the order of the joins affects what the conditions hash
applies too. I believe this unintuitive.

I think a better system would be to use the names of the

relationships to dictate the search criteria

This would stop the user having to guess what the join table is

called.

Person.find(:all, :joins =>
[:work_address, :home_address], :conditions => {:home_address =>
{:street_no => 22}})

If this could be developed I would think we’d need to cover these

cases:

1. Eager loading using inner join

Person.find(:all, :joins =>
[:work_address, :home_address], :conditions => {:home_address =>
{:street_no => 22}})

2. Eager loading using left join

Notice that the home_address has conditions specified so in this

case we might want to enforce a left join
Person.find(:all, :include =>
[:work_address, :home_address], :conditions => {:home_address =>
{:street_no => 22}})

3.No eager loading specified (would we force a join because of the

conditions specified?)
Person.find(:all, :conditions => {:home_address => {:street_no =>
22}})

4. Custom SQL for each nesting, so that you can use any sql but not

have to explicitly know the name of the table
Person.find(
:all,
:joins => :home_address
:conditions => [
{:age => 22},
# This would currently conflict with the currect nested hash
since
{:home_address => [‘home_address.street_name LIKE ?’, ‘Dougla%’]}
]
)

1. Joins

SELECT *

FROM people

JOIN addresses home_address ON (people.home_address_id =

home_address.id)

WHERE people.age = 22 AND

home_address.street_name LIKE ‘Dougla%’

2. Left Joins (:include)

SELECT *

FROM people

WHERE people.age = 22

SELECT *

FROM addresses home_address

WHERE home_address.street_name LIKE ‘Dougla%’

AND home_address.id IN (1,2,3,4,5,6,7,8,9,10)

I’d like to hear your thoughts and ideas on any inherent problems with
getting this implemented, or design improvements.

It’s only an idea at this stage, but I’d like to develop it so that
developing complex dynamic searches using rails
becomes trivial.

i would post it in the rails core group.

cheers, i made it a bit more clear and posted it in rails core