Has_many_and_belongs_to preloads all data -> how can I do to not to do it?

Hello,

I have two tables:

  class Person < ActiveRecord::Base
     has_and_belongs_to_many :projects
  end

  class Project < ActiveRecord::Base
     has_and_belongs_to_many :people
  end

Then I want to get people working on a project. I do:

  project_record.people.find_all_by_foobar( foobar )

It works, but it internally loads ALL the people working on the project,
not
only “foobar” people.

It seems that just using “project_record.people” loads everything into
memory.
Is there any workaround how to load only what is really needed? Lets
imagine
there is huge amount of projects and you just want to check if a person
works
on a certain project…

Thank you,
Jan

On 1/2/08, mig [email protected] wrote:

     has_and_belongs_to_many :people

Is there any workaround how to load only what is really needed? Lets imagine
there is huge amount of projects and you just want to check if a person works
on a certain project…

Have you considered something like this?

project = Project.find( 1 )
person = People.find( 1 )
person.projects.include? project


Greg D.
http://destiney.com/

Take a look at

For many-to-many associations, there are no dynamic finder proxy
methods.

Use

project_record.people.find(:all, :conditions => { :foobar =>
“foobar” })

instead.