# Has\_many + finder\_sql & :include

**URL:** <https://www.ruby-forum.com/t/has-many-finder-sql-include/64997>\
**Category:** Rails\
**Created:** [August 15, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/has-many-finder-sql-include/64997 "2006-08-15T16:39:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@roland](https://www.ruby-forum.com/u/roland)\
**Post date:** [August 15, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/has-many-finder-sql-include/64997/1 "2006-08-15T16:39:16Z")

</div>

Hello,

In a model I have:

has\_many :things, :finder\_sql =\> ’ select \* from other\_things ’ +  
’ where id = #{id} ’

When I do MyClass.find(:all, :include =\> :things) I get a weird error:

from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/abstract\_adapter.rb:120:in  
`log' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/sqlserver_adapter.rb:485:in`select’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/sqlserver\_adapter.rb:233:in  
`select_all' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:1110:in`select\_all\_rows’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations.rb:975:in  
`find_with_associations' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:973:in`find\_with\_associations’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:923:in  
`find_every' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:381:in`find’

Any ideas?

Roland

---

<div class="post-metadata">

**Author:** ![roland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@roland](https://www.ruby-forum.com/u/roland)\
**Post date:** [August 15, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/has-many-finder-sql-include/64997/2 "2006-08-15T17:41:55Z")

</div>

Roland M. wrote:

> c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/abstract\_adapter.rb:120:in  
> `log’

It’s not possible to include a collection that has a custom finder SQL.

You’ll have make the whole query a custom MyClass.find\_by\_sql query,  
using the [http://mrj.bpa.nu/eager\_custom\_sql.rb](http://mrj.bpa.nu/eager_custom_sql.rb) mod to attach the  
OtherThing objects to the MyClass objects.

But is your custom SQL the same as specifying the :class\_name to be  
‘OtherThings’, and the :foreign\_key to be ‘id’?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![roland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@roland](https://www.ruby-forum.com/u/roland)\
**Post date:** [August 15, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/has-many-finder-sql-include/64997/3 "2006-08-15T20:54:33Z")

</div>

On 8/15/06, Roland M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> from  
> `select\_all’  
> /lib/active\_record/associations.rb:973:in  
> Any ideas?  
> Using :finder\_sql rules out all the other sql modifiers since Rails  
> doesn’t  
> know how to parse and modify the SQL you provide. This does deserve a  
> clear  
> error message.

jeremy
