On 19 Jan 2009, at 09:30, Greg Hauptmann wrote:

> PS.  Here's example of the SQL code used to give some more details  
> (have used different model names).  Again the question is whether  
> there is a way with ActiveRecord to construct the query "return all  
> the BankAccount's for which they have more than 100 AccountItem's".   
> (i.e. using "through", named scopes or whatever)
>
> SELECT ba.* FROM bank_accounts AS ba
> LEFT JOIN account_items AS ai
>      ON ai.bank_account_id = ba.id
> GROUP BY ba.id
> HAVING COUNT(ai.id) > 100
>
>

BankAccounts.find :all, :select => 'bank_accounts.*', :joins =>  
'account_items', :group => 'bank_accounts.id HAVING COUNT(ai.id) > 100'

should do the trick. Edge has a :having option I think, but you can  
fudge it into the group clause

Fred
                                                                                
                                        
> Thanks
>
> On Mon, Jan 19, 2009 at 5:21 PM, Greg Hauptmann 
> <[email protected] 
> > wrote:
> Hi,
>
> Can I get an ActiveRecord.find to do this query?:   Get all Articles  
> which have only been published in less than X Magazines.
>
> Model Relationship:  "Magazine" has_and_belongs_to_many "Articles"s.
>
> thanks
>
>
>
>
>
>
> -- 
> Greg
> http://blog.gregnet.org/
>
>
>
>
>
> -- 
> Greg
> http://blog.gregnet.org/
>
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to