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
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
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 Mon, Feb 15, 2010 at 11:49 PM, Greg Ma <gregorylepacha@msn.com> 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
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
···
On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma <gregorylepacha@msn.com> > wrote:
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)
···
On Tue, Feb 16, 2010 at 12:17 AM, Greg Ma <gregorylepacha@msn.com> wrote:
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
--
Posted via http://www.ruby-forum.com/\.