Find by association table

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
:slight_smile:

#method
def find_student_by_tag(tag)
result = Tag.find_by_name(:first, :conditions => [β€œname LIKE ?”,
β€œ%#{tag}%”])
students = Student.find(:all)

students.each do |student|
  students.delete(student) unless

students.tags.find_by_name(result.name)
end
students
end

Greg

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
:slight_smile:

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

On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma [email protected]
wrote:

#method

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 (
Active Record Associations β€” Ruby on Rails Guides).

If this is the case, then you should be able to simply say:
def find_students_by_tag(tag)
tag.students
end

On Tue, Feb 16, 2010 at 12:17 AM, Greg Ma [email protected]
wrote:

Have you looked at the acts_as_taggable plugin?

That page has an example posts = Post.find_tagged_with(β€˜tag1’)

Which looks like you should be able to say

Student.find_tagged_with(tag)

Rather than the method you are currently trying to create
Student.find_students_by_tag(tag)

(actually, if you were wanting to call it like that, then it should be
defined β€œdef self.find_students_by_tag” because it should be a method on
the
Student class rather than on some particular student)

Josh C. wrote:

On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma [email protected]
wrote:

#method

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 (
Active Record Associations β€” Ruby on Rails Guides).
It is a he-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

No i cannot do this because the parameter isnt a tag object but just a
string.
I guess i could try this
def find_students_by_tag(tag)
res = Tag.find_by_name(tag)
res.students unless res
end