it sounds like you want to do a select, e.g.
@directors = Director.find(:all).select {|director| !(0 == director.movies.count)}
-faisal
it sounds like you want to do a select, e.g.
@directors = Director.find(:all).select {|director| !(0 == director.movies.count)}
-faisal
@directors = Director.find(:all).select {|director| !(0 ==
director.movies.count)}
This method is still going to pull all of the directors from the database before filtering. I'd favour a more SQL heavy approach:
Director.find(:all, :joins => 'inner join movies on movies.director_id = directors.id', :group => 'directors.id')
SQL would naturally filter out the directors with no movies as the inner join requires the director to exist in the movies table. You then group the results so you just get the directors and not the associated movies.
This is obviously more verbose than a straight find(:all) but you can just add a method to your model. If you find yourself using this across a few of your models, you can try and create a method you can use to extend active record.
Hope that helps,
Steve