# Complex find

**URL:** <https://www.ruby-forum.com/t/complex-find/172840>\
**Category:** Rails\
**Created:** [July 28, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/complex-find/172840 "2009-07-28T19:32:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 28, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/complex-find/172840/1 "2009-07-28T19:32:45Z")

</div>

I have

class Debtortrans \< ActiveRecord::Base  
has\_many :debtortranstaxes, :foreign\_key =\>  
‘debtortransid’, :class\_name =\> ‘debtortranstaxes’  
belongs\_to :debtor, :foreign\_key =\> ‘debtorno’

and

class Debtortranstaxes \< ActiveRecord::Base  
belongs\_to :debtortrans, :foreign\_key =\> ‘debtortransid’

Now I want to find Debtortrans records but only those with specific  
Debtortranstaxes values…

@debtor = Debtortrans.find(:all,  
:conditions =\> [“trandate \> ? and trandate \< ? AND  
debtortranstaxes.taxauthid = ?”, @per1, @per.lastdate\_in\_period, “24”],  
:include =\> ‘debtortranstaxes’,  
:select =\> ‘debtorno, trandate, transno, ovamount’)

but this gives me an error…  
NameError: undefined local variable or method `debtortranstaxes’ for  
#Class:0xb7a4e644

so I change the conditions to  
:conditions =\> [“trandate \> ? and trandate \< ? AND taxauthid = ?”,  
@per1, @per.lastdate\_in\_period, “24”],

and I get this error…  
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column ‘taxauthid’  
in ‘where clause’: SELECT debtorno, trandate, transno, ovamount FROM  
`debtortrans` WHERE (trandate \> ‘2009-04-30’ and trandate \< ‘2009-05-31’  
AND taxauthid = ‘24’)

obviously because taxauthid column is not in debtortrans table but in  
the ‘has\_many’ table of debtortranstaxes.

How do I get the search of debtortrans with only appropriate records in  
debtortranstaxes table?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 28, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/complex-find/172840/2 "2009-07-28T21:33:56Z")

</div>

On Tue, 2009-07-28 at 10:31 -0700, Craig W. wrote:

> belongs\_to :debtortrans, :foreign\_key =\> ‘debtortransid’  
> but this gives me an error…  
> `debtortrans` WHERE (trandate \> ‘2009-04-30’ and trandate \< ‘2009-05-31’  
> AND taxauthid = ‘24’)
> 
> obviously because taxauthid column is not in debtortrans table but in  
> the ‘has\_many’ table of debtortranstaxes.
> 
> How do I get the search of debtortrans with only appropriate records in  
> debtortranstaxes table?

* * *

I got this to work…had to use ‘joins’ instead of ‘include’ - duh

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.
