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:
Is this a known issue?
If so, is it resolved in a later version of Rails?
It appears that providing the :finder_sql option on the has_many
declaration may help - any thoughts on that?