From: Josh Cheek Date: 2010-02-16T15:10:26+09:00 Subject: Re: Find by association table --000e0cd139c4d45885047fb19337 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma wrote: > Greg Ma wrote: > > Hi, > > I did a the method below that return all students who has the method > > parameter in their tags collection. > > I am sure this is pretty by coded, could someone help clean it up please > > :) > > > > I correct myself, but there is still probably something cleaner possible > > #method > def find_student_by_tag(tag) > students = Student.find(:all) > > students.each do |student| > students.delete(student) unless > students.tags.find_by_name(tag) > end > students > end > > > > Greg > > -- > Posted via http://www.ruby-forum.com/. > > Based on the similar topic to your previous post, which seemed to be a Rails application (if you recall, I suggested you would have more luck at the Rails mailing list http://groups.google.com/group/rubyonrails-talk ), I would suggest that you should have an association between students and tags. Probably a has_and_belongs_to_many association through a join table ( http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association). If this is the case, then you should be able to simply say: def find_students_by_tag(tag) tag.students end --000e0cd139c4d45885047fb19337--