Forum: Ruby on Rails Finding by association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jason F. (Guest)
on 2006-01-12 05:16
I have two objects that are associated in the following way:

  class Class1 < ApplicationController
    has_and_belongs_to_many :class2s
    has_and_belongs_to_many :otherClasses
  end

  class Class2 < ApplicationController
    has_and_belongs_to_many :class1s
    has_and_bleongs_to_many :otherClasses
  end

Now, when I run the following query,

  @objects = Class1.find(:all, :conditions => ["class2s.id = ?",
params[:id]], :include => :class2s)

I expect to get a collection of Class1 objects wihere each Class1 object
would have a fully loaded collection of Class2 objects (since I asked to
:include => :class2s).  However, instead, I get a collection of Class1
objects that only have a single Class2 object in their class2s
collection (the Class2 object with the id that I specified in the
conditions clause).

I'm thinking this might have something to do with the habtm bug that is
floating around, but am not sure.  Am I doing this right?  Any ideas?

- Jason
M Daggett (Guest)
on 2006-01-12 08:08
(Received via mailing list)
Hi Jason,
I could be totally missing your point but I think that rails provides
you the ability to do this
@o = myClass1.class2s

@o will now contain an array of all the class2 objects collected in
that instance of myClass1


Mark

On 1/11/06, Jason F. <removed_email_address@domain.invalid> wrote:
>   end
> collection (the Class2 object with the id that I specified in the
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Jason F. (Guest)
on 2006-01-12 16:52
What I'm trying to do is find all Class1 objects that have an
association to Class2 objects.  However, I also want to make sure that
the Class1 objects that are returned by the find() method have fully
populated Class2 collections, i.e., I want it to depend on lazy loading
to get me my Class2 collection in my Class1 objects.

M Daggett wrote:
> Hi Jason,
> I could be totally missing your point but I think that rails provides
> you the ability to do this
> @o = myClass1.class2s
>
> @o will now contain an array of all the class2 objects collected in
> that instance of myClass1
>
>
> Mark
>
> On 1/11/06, Jason F. <removed_email_address@domain.invalid> wrote:
>>   end
>> collection (the Class2 object with the id that I specified in the
>> Rails mailing list
>> removed_email_address@domain.invalid
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
> --
This topic is locked and can not be replied to.