Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Find by association table

Reply
Thread Tools

Find by association table

 
 
Greg Ma
Guest
Posts: n/a
 
      02-16-2010
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Greg Ma
Guest
Posts: n/a
 
      02-16-2010
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/.

 
Reply With Quote
 
 
 
 
Josh Cheek
Guest
Posts: n/a
 
      02-16-2010
[Note: parts of this message were removed to make it a legal post.]

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/associ...ny-association).

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

 
Reply With Quote
 
Greg Ma
Guest
Posts: n/a
 
      02-16-2010
Josh Cheek wrote:
> On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma <>
> 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 (
> http://guides.rubyonrails.org/associ...ny-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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      02-16-2010
[Note: parts of this message were removed to make it a legal post.]

On Tue, Feb 16, 2010 at 12:17 AM, Greg Ma <> 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/.
>
>

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Update unable to find TableMapping['Table'] or DataTable 'Table' in c# sqlserver Nishanth Software 0 11-09-2009 09:58 AM
Single Table Inheritance Association Issue Mike Ruby 1 03-12-2007 04:18 PM
How can I find the name of the parent table from a table cell? jklimek@gmail.com Javascript 7 07-10-2005 11:27 PM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table. David Williams ASP .Net 2 08-12-2003 07:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57