On Sep 22, 9:35 pm, Ben Johnson <[EMAIL PROTECTED]>
wrote:
> Frederick Cheung wrote:
> > On Sep 22, 9:16 pm, Ben Johnson <[EMAIL PROTECTED]>
> > wrote:
> Thanks for your help. I agree, I would prefer to use joins, but they do
> not return the proper results. It seems that joins return duplicate
> results:
>
> >> User.find(:all, :conditions => "orders.id > 1", :joins => :orders).size
>
> => 1254
>
> >> User.find(:all, :conditions => "orders.id > 1", :include => :orders).size
>
> => 934
>
> In the above example the first query is returning duplicate results. I
> am getting users with the same ID. I'm confused why it would return
> duplicate results. Any idea why this is? Seeing as this is a problem it
> seems that :include is my only option.
If your users table had exactly one user, and that user had 2 orders,
then
select * from users inner join orders on orders.user_id = users.id
would return the result set (i've made up some column names)
users.id | users.name | orders.id | orders.name|
1 | bob | 1 | a tv
1 | bob | 2 | a dvd
This is just how joins are supposed to work. I'm still not sure what
you're trying to achieve in the first place (but grouping or using
select distinct may be options)
:include is different (and doesn't even use joins half the time). It's
doing a lot of work behind the scenes wiring up associations.
Fred
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---