# Has\_many\_and\_belongs\_to preloads all data -\> how can I do to not to do it?

**URL:** <https://www.ruby-forum.com/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/124816>\
**Category:** Rails\
**Created:** [January 2, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/124816 "2008-01-02T21:34:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 2, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/124816/1 "2008-01-02T21:34:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 2, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/124816/2 "2008-01-02T23:42:51Z")

</div>

On 1/2/08, mig [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 3, 2008, 1:14am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/124816/3 "2008-01-03T01:14:54Z")

</div>

Take a look at

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

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

Use

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

instead.
