Don't pull db data in views - that's the controller's job. In your views, you should only reference data loaded by your controller method.
In your case, that means creating the artist list, using :include
so the songs are pre-loaded.
# In your controller
@artists = Artist.find(:all, :include => :songs)
EDIT In response to method's comment, there are two issues with pulling data from views:
- Separation of concerns. This is what I was talking about when I said you "should" only pull data in the controller - in MVC the controller handles data access and, in theory, the views don't contain logic. It's certainly easier to put some data retrieval in your views at times, but it gets ugly pretty quick.
- Efficiency, either in number of queries or query result size, which I think is what method was talking about. If you're loading enough data in a single page that this becomes an issue, I wonder if that doesn't suggest some refactoring (or at least pagination).