# Many To Many Relationship help needed

**URL:** <https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254>\
**Category:** Rails\
**Created:** [February 20, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254 "2007-02-20T23:37:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mooktakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@mooktakim](https://www.ruby-forum.com/u/mooktakim)\
**Post date:** [February 20, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254/1 "2007-02-20T23:37:20Z")

</div>

Hello,

I have 3 models:

class Result \< ActiveRecord::Base  
has\_many :collections  
has\_many :articles, :through =\> :collections  
end

class Article \< ActiveRecord::Base  
has\_many :collections  
has\_many :results, :through =\> :collections  
end

class Collection \< ActiveRecord::Base  
belongs\_to :result  
belongs\_to :article  
end

Collection has a table like this:  
create\_table :collections do |t|  
t.column :id, :integer  
t.column :result\_id, :integer  
t.column :article\_id, :integer  
t.column :extract, :text  
end

The problem i have is this.  
I do a find for a single result.  
Then i go through each article in this result, using the each method.

At this point i’m outputting all the attributes of articles.  
But i want to also output the extract for this article and result.

How would i do this?

article.extract doesn’t work, nor does article.results.extract

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![mooktakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@mooktakim](https://www.ruby-forum.com/u/mooktakim)\
**Post date:** [February 20, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254/2 "2007-02-20T23:51:21Z")

</div>

In your design article has many collections therefor article also has  
many extracts. Which means that article will not have an extract  
method which is why article.extract will not work.

In the same way article.results method will give you an Array of  
Result object so obviously will not have an extract method. This is  
why articles.results.extract will not work.

Maybe that will help you rethink your design to produce your desired  
results.

---

<div class="post-metadata">

**Author:** ![mooktakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@mooktakim](https://www.ruby-forum.com/u/mooktakim)\
**Post date:** [February 20, 2007, 11:54pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254/3 "2007-02-20T23:54:21Z")

</div>

Oops slight typo in previous post

articles.results.extract

should have been

article.results.extract

---

<div class="post-metadata">

**Author:** ![mooktakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@mooktakim](https://www.ruby-forum.com/u/mooktakim)\
**Post date:** [February 21, 2007, 12:27am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254/4 "2007-02-21T00:27:25Z")

</div>

Thanks.  
But, won’t this run SQL query for each article in result?

There must be an easy way to combine both article and result, using  
collection.

I’ll use this for now, until i find something better.

On Tue, February 20, 2007 10:58 pm, Jon G. wrote:

> 

> > end
> > 
> > How would i do this?
> 
> >

–  
Website: [http://www.mooktakim.com](http://www.mooktakim.com)  
email: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mooktakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@mooktakim](https://www.ruby-forum.com/u/mooktakim)\
**Post date:** [February 21, 2007, 12:00am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254/5 "2007-02-21T00:00:22Z")

</div>

You’ll need to find the collection and use collection.extract. Easiest  
might be something like…

result.articles.each do |article|  
collection = article.collections.find(:first, :conditions =\>  
[“result\_id = ?”, result.id])  
end

This assumes that there’s only ever one collection associated with any  
article/result pair.

---

<div class="post-metadata">

**Author:** ![mooktakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@mooktakim](https://www.ruby-forum.com/u/mooktakim)\
**Post date:** [February 21, 2007, 12:30am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254/6 "2007-02-21T00:30:25Z")

</div>

I understand. I thought because i’ve combined both article and result  
using collection,  
the extract attribute might be included in both.

There must be a nice and simple way to do this, without going through  
every article, or  
result.

Anyway, thanks for your help.

On Tue, February 20, 2007 10:53 pm, Robert W. wrote:

> 

> On Feb 20, 5:50 pm, “Robert W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > results.
> > 
> > > class Result \< ActiveRecord::Base has\_many :collections has\_many :articles, :through  
> > > create\_table :collections do |t| t.column :id, :integer t.column :result\_id, :integer

–  
Website: [http://www.mooktakim.com](http://www.mooktakim.com)  
email: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mooktakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@mooktakim](https://www.ruby-forum.com/u/mooktakim)\
**Post date:** [February 21, 2007, 11:24am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-help-needed/86254/7 "2007-02-21T11:24:18Z")

</div>

use eager loading. That will only take one SQL query, and you will  
have all the stuff you need in your result.  
(code not tested and probably contains typos 😉 )

#controller  
@result = Result.find :first, :conditions =\> [someting], :include =\>  
{:collections =\> :articles }

#view  
\<%= @result.collections.each do |coll| %\>  
… access the extract…  
\<%= coll.extract %\>  
…access the article attributes with:…  
\<%= coll.article.some\_attribute %\>  
end
