Find by association table

G

Greg Ma

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


#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
 
G

Greg Ma

Greg said:
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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
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
 
G

Greg Ma

Josh said:
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).
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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
Have you looked at the acts_as_taggable plugin?
http://juixe.com/techknow/index.php/2006/07/05/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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top