# Help With Pre-Loading

**URL:** <https://www.ruby-forum.com/t/help-with-pre-loading/47614>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/help-with-pre-loading/47614 "2005-11-12T12:49:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/help-with-pre-loading/47614/1 "2005-11-12T12:49:41Z")

</div>

I need a little bit of help to determine if pre-loading could help me  
solve  
a performance problem.

I have the following models:

Band (has\_many Releases)  
Release (has\_many ReleaseTracks)  
ReleaseTrack (has\_many AudioFiles)  
AudioFile

So, the situation arises that I want to see all the AudioFiles for a  
band.  
Since the band and the audio are several associations away from each  
other,  
would pre-loading even work in this situation?

I am having performance problems when writing code to answer the  
following  
question:

How many audio files does a band have?

Right now I am getting the releases, getting the tracks, and iterating  
through the audio files. This takes a long time. Since the associations  
are  
so far apart, I don’t think the counter\_cache thing will work, right?

If anyone has any suggestions as to how I could improve things, that  
would  
be great. Every time I ask a question like this I find out Rails has  
some  
kind of special feature that solves my problem.

Maybe I’ll get lucky again now?

Thx,  
Hunter

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/help-with-pre-loading/47614/2 "2005-11-12T12:49:42Z")

</div>

You can try to denormalize your schema by adding band\_id to the  
audio\_file and make sure you set that everytime you create it.

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/help-with-pre-loading/47614/3 "2005-11-12T12:49:44Z")

</div>

I am not the greatest of SQL gurus but I am sure you could do it more  
efficiently inside the database using joins of some sort. Something  
like

SELECT audio\_files.\*  
FROM audio\_files  
RIGHT JOIN release\_tracks ON release\_tracks.id =  
audio\_files.release\_track\_id  
RIGHT JOIN releases ON releases.id = release\_tracks.release\_id  
WHERE releases.band\_id = 1

may select the audio\_files for a specific band … or maye complete  
gibberish more likely but you should get the idea. Then in your code  
you can do something like the following

AudioFile.find(:all, :conditions =\> [‘releases.band\_id = ?’, 1],  
:joins =\> 'RIGHT JOIN release\_tracks ON release\_tracks.id =  
audio\_files.release\_track\_id RIGHT JOIN releases ON releases.id =  
release\_tracks.release\_id ', :select =\> ‘audio\_files.\*’ )

or

AudioFile.count( [‘releases.band\_id = ?’, 1], 'RIGHT JOIN  
release\_tracks ON release\_tracks.id = audio\_files.release\_track\_id  
RIGHT JOIN releases ON releases.id = release\_tracks.release\_id ’ )

Hope that helps,

Peter D.

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/help-with-pre-loading/47614/4 "2005-11-12T12:49:44Z")

</div>

Hunter,

Just to expand on Peter’s suggestion…(careful I’m a noob) You could  
wrap  
the request in another method to make more DRY. You could make this a  
method  
of the model so that in your controller or view you could say

@my\_band.count\_audio\_files

and include the code that Peter suggests in there

ie  
def self.count\_audio\_files  
AudioFile.count( [‘releases.band\_id = ?’, self], 'RIGHT JOIN  
release\_tracks ON release\_tracks.id = audio\_files.release\_track\_id  
RIGHT JOIN releases ON releases.id [http://releases.id/](http://releases.id/) =  
release\_tracks.release\_id ’ )  
end

I haven’t tested this… I hope it’s the right way to do it 🙂

Cheers
