Filter nested active records

exercise hm:t muscles

In my controller I do @muscle_groups = MuscleGroup.all

Then in my view I do
- @muscle_groups.each do |mg|
  - mg.muscles.all(:conditions => ["id not in (?)",
@exercise.muscle_ids]).each do |m|

I'm trying to filter out all the muscles already added to an exercise.
However this is clear bad practice. Since all this should be happening
in the controller (prehaps even in the model?)

So how do I go about filtering my active record based on values by
association? if that makes sense...

best regards,

Can you be more specific about what your model associations look like?
What I would suggest is trying to come up with a way to push all of
this logic down to mysql, so basically you would want to query
straight from the Muscle model. Muscle.find(:all, :conditions =>
"", :include => :muscle_groups), etc. I dunno the exact details of
your database but a good combination of :include or :join should solve
your issue. Have a look at the documentation in rails and look at your
development logs to see what queries are being generated when you hit
the view to get an idea of what everything is doing.