# ActiveRecord Nested Conditions Improvements Suggestion

**URL:** <https://www.ruby-forum.com/t/activerecord-nested-conditions-improvements-suggestion/162902>\
**Category:** Rails\
**Created:** [March 15, 2009, 12:19pm UTC](https://www.ruby-forum.com/t/activerecord-nested-conditions-improvements-suggestion/162902 "2009-03-15T12:19:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zyclops](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7feea3/32.png) [@zyclops](https://www.ruby-forum.com/u/zyclops)\
**Post date:** [March 15, 2009, 12:19pm UTC](https://www.ruby-forum.com/t/activerecord-nested-conditions-improvements-suggestion/162902/1 "2009-03-15T12:19:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zyclops](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7feea3/32.png) [@zyclops](https://www.ruby-forum.com/u/zyclops)\
**Post date:** [March 16, 2009, 12:27pm UTC](https://www.ruby-forum.com/t/activerecord-nested-conditions-improvements-suggestion/162902/2 "2009-03-16T12:27:39Z")

</div>

i would post it in the rails core group.

---

<div class="post-metadata">

**Author:** ![zyclops](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7feea3/32.png) [@zyclops](https://www.ruby-forum.com/u/zyclops)\
**Post date:** [March 16, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/activerecord-nested-conditions-improvements-suggestion/162902/3 "2009-03-16T13:39:18Z")

</div>

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