# "build" is slow on large has\_many association collections

**URL:** <https://www.ruby-forum.com/t/build-is-slow-on-large-has-many-association-collections/121692>\
**Category:** Rails\
**Created:** [November 29, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/build-is-slow-on-large-has-many-association-collections/121692 "2007-11-29T23:48:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 29, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/build-is-slow-on-large-has-many-association-collections/121692/1 "2007-11-29T23:48:56Z")

</div>

All,

Rails 1.1.6

Given: an AR::Base descendant X has\_many :Ys

As the size of the collection of Ys increases, the time it takes to run  
the “build” method (used to create a new instance of Y, as in  
x.ys.build(:attr1 =\> ‘blah’) takes longer and longer.

For my specific case, if I call x.ys.build(:attr1 =\> ‘blah’):

If X has 1000 Ys, it takes about 2 seconds to build a new Y  
If X has 42000 Ys, it takes about 90 seconds to build a new Y

I’ve tracked down the issue to the “find\_target” method in  
has\_many\_association.rb.

It _looks_ like Rails is trying to determine if the object (the new Y)  
that the user is requesting to be created already exists in the  
collection and somehow this is less than optimal.

A couple of questions:

1. Is this a known issue?
2. If so, is it resolved in a later version of Rails?
3. It appears that providing the :finder\_sql option on the has\_many  
declaration may help - any thoughts on that?

Thanks in advance,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 30, 2007, 12:48am UTC](https://www.ruby-forum.com/t/build-is-slow-on-large-has-many-association-collections/121692/2 "2007-11-30T00:48:31Z")

</div>

I found that the call to “load\_target” in method “build” (defined in  
has\_many\_associations.rb) was not necessary.

A quick look at the Rails 1.2 AR code makes it look like this is no  
longer a problem in those versions.

But it was definitely do a find(:all) [ and using the :finder\_sql option  
didn’t change that ] on the collection class (Y), for apparently, no  
reason.

I guess no one’s on 1.1.6 anymore ;), but just in case, you could monkey  
patch your way out of this one.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 30, 2007, 1:26am UTC](https://www.ruby-forum.com/t/build-is-slow-on-large-has-many-association-collections/121692/3 "2007-11-30T01:26:24Z")

</div>

Yep. I believe this issue crept up again in 1.2.5 but was removed in  
1.2.6. It was loading the entire association unnecessarily.

-Bill

Wes G. wrote:

> I guess no one’s on 1.1.6 anymore ;), but just in case, you could monkey  
> patch your way out of this one.
> 
> Wes

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 30, 2007, 1:28am UTC](https://www.ruby-forum.com/t/build-is-slow-on-large-has-many-association-collections/121692/4 "2007-11-30T01:28:49Z")

</div>

William P. wrote:

> Yep. I believe this issue crept up again in 1.2.5 but was removed in  
> 1.2.6. It was loading the entire association unnecessarily.
> 
> -Bill
> 
> Wes G. wrote:
> 
> > I guess no one’s on 1.1.6 anymore ;), but just in case, you could monkey  
> > patch your way out of this one.
> > 
> > Wes
> 
> –  
> Sincerely,
> 
> William P.

It’s crazy, because you would see slowness super quickly as your DB  
grew. Surprised that it made it back in.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 30, 2007, 1:57am UTC](https://www.ruby-forum.com/t/build-is-slow-on-large-has-many-association-collections/121692/5 "2007-11-30T01:57:57Z")

</div>

I know. I believe this is the TRAC ticket:

[http://dev.rubyonrails.org/ticket/8713](http://dev.rubyonrails.org/ticket/8713)

Wes G. wrote:

> > > patch your way out of this one.
> 
> It’s crazy, because you would see slowness super quickly as your DB  
> grew. Surprised that it made it back in.
> 
> Wes

–  
Sincerely,

William P.
