# Find by association table

**URL:** <https://www.ruby-forum.com/t/find-by-association-table/183533>\
**Category:** Ruby\
**Created:** [February 16, 2010, 6:47am UTC](https://www.ruby-forum.com/t/find-by-association-table/183533 "2010-02-16T06:47:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 6:47am UTC](https://www.ruby-forum.com/t/find-by-association-table/183533/1 "2010-02-16T06:47:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 6:49am UTC](https://www.ruby-forum.com/t/find-by-association-table/183533/2 "2010-02-16T06:49:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:10am UTC](https://www.ruby-forum.com/t/find-by-association-table/183533/3 "2010-02-16T07:10:49Z")

</div>

On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> #method
> 
> > Greg
> 
> –  
> Posted via [http://www.ruby-forum.com/](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](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](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

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:33am UTC](https://www.ruby-forum.com/t/find-by-association-table/183533/4 "2010-02-16T07:33:39Z")

</div>

On Tue, Feb 16, 2010 at 12:17 AM, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

Have you looked at the acts\_as\_taggable plugin?

> **[Acts As Taggable Plugin](http://juixe.com/techknow/index.php/2006/07/05/acts-as-taggable-plugin/)**
>
> The first Ruby on Rails plugin that I used was acts\_as\_taggable. With the acts\_as\_taggable plugin you can tag instances of your models in the same fashion you tag your pictures on Flickr or your bo…

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)

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 16, 2010, 7:17am UTC](https://www.ruby-forum.com/t/find-by-association-table/183533/5 "2010-02-16T07:17:21Z")

</div>

Josh C. wrote:

> On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > #method
> > 
> > > Greg
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](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](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](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
