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


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

Have you considered something like this?

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

Take a look at

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


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