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, Seb -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
lancecarlson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Nov-30 08:44 UTC
Re: Filter nested active records
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. On Nov 28, 11:51 am, Seb <sebastianthegreat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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, > Seb-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.