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

**URL:** <https://discuss.rubyonrails.org/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/22568>\
**Category:** rubyonrails-talk\
**Created:** [January 2, 2008, 8:34pm UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/22568 "2008-01-02T20:34:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mig](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mig](https://discuss.rubyonrails.org/u/mig)\
**Post date:** [January 2, 2008, 8:34pm UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/22568/1 "2008-01-02T20:34:12Z")

</div>

Hello,

I have two tables:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Person \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :projects &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Project \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :people &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:** ![Greg\_Donald1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/greg_donald1/32/1313_2.png) [@Greg\_Donald1](https://discuss.rubyonrails.org/u/Greg_Donald1)\
**Post date:** [January 2, 2008, 10:42pm UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/22568/2 "2008-01-02T22:42:34Z")

</div>

Have you considered something like this?

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

---

<div class="post-metadata">

**Author:** ![Java](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Java](https://discuss.rubyonrails.org/u/Java)\
**Post date:** [January 3, 2008, 12:14am UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-preloads-all-data-how-can-i-do-to-not-to-do-it/22568/3 "2008-01-03T00:14:30Z")

</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

&nbsp;&nbsp;project\_record.people.find(:all, :conditions =\> { :foobar =\> "foobar" })

instead.
