# Problem in has\_and\_belongs\_to\_many data retrieval

**URL:** <https://www.ruby-forum.com/t/problem-in-has-and-belongs-to-many-data-retrieval/120617>\
**Category:** Rails\
**Created:** [November 19, 2007, 8:31am UTC](https://www.ruby-forum.com/t/problem-in-has-and-belongs-to-many-data-retrieval/120617 "2007-11-19T08:31:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamhei82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@kamhei82](https://www.ruby-forum.com/u/kamhei82)\
**Post date:** [November 19, 2007, 8:31am UTC](https://www.ruby-forum.com/t/problem-in-has-and-belongs-to-many-data-retrieval/120617/1 "2007-11-19T08:31:52Z")

</div>

Hi All,

I’m new in RoR, I have a problem in retrieving data in  
has\_and\_belongs\_to\_many relationship.

I have created 3 tables, Tag, Entry and Entries\_Tags .  
There are join in has\_and\_belongs\_to\_many

I can successfully create new record by this relationship. But I cannot  
retrieve the data.

I want to do a task to select all Entries with condition "name = ‘g’ in  
Tag table.

I use this code to select all the record with name = ‘g’  
@resulttag = Tag.find(:all, :conditions =\> “name = ‘g’”)

Then I want to select all related Entries record, by this code  
@r = @resulttag.entries

But it is not work, @r still storing the same data with @resulttag (The  
array storing Tag records, not Entry records)

Can anyone can help or provide a solution to do such task ???

Thanks a lot,  
Stanley

=================CODE===========================================  
def showresult

```
@resulttag = Tag.find(:all, :conditions => "name = 'g'")
logger.info("Size = #{@resulttag.size}")
@r = @resulttag.entries
logger.info("RSize = #{@r.size}")

@resulttag.entries.each do |en|
  logger.info("RES = #{en.id}")

```

end

==================LOG===========================================  
Size = 8  
RSize = 8  
e[4;35;1mTag Columns (0.011401)e[0m e[0mSHOW FIELDS FROM tagse[0m  
RES = 10  
RES = 11  
RES = 12  
RES = 13  
RES = 14  
RES = 16  
RES = 17  
RES = 18

---

<div class="post-metadata">

**Author:** ![kamhei82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@kamhei82](https://www.ruby-forum.com/u/kamhei82)\
**Post date:** [November 19, 2007, 8:53am UTC](https://www.ruby-forum.com/t/problem-in-has-and-belongs-to-many-data-retrieval/120617/2 "2007-11-19T08:53:58Z")

</div>

On 19 Nov 2007, at 07:31, Stanley Wong wrote:

> 

It’s not working because @resulttag is an array of tags, not a single  
tag  
@resulttag.entries.collect(&:entries) will give you an array  
containing an array for each tag, or if you want an array with all  
entries,  
@resulttag.entries.collect(&:entries).flatten will do the job

You could also approach it the other way round:

Entry.find\_by\_sql \<\<\_SQL  
SELECT \* from entries  
INNER JOIN entries\_tags on entries.id = entry\_id  
INNER JOIN tags on tags.id = tag\_id  
where name = ‘g’  
\_SQL

Fred

---

<div class="post-metadata">

**Author:** ![kamhei82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@kamhei82](https://www.ruby-forum.com/u/kamhei82)\
**Post date:** [November 19, 2007, 9:02am UTC](https://www.ruby-forum.com/t/problem-in-has-and-belongs-to-many-data-retrieval/120617/3 "2007-11-19T09:02:27Z")

</div>

Hi Fred,

Thx, it’s work

Stanley

Frederick C. wrote:

> On 19 Nov 2007, at 07:31, Stanley Wong wrote:
> 
> > 
> 
> It’s not working because @resulttag is an array of tags, not a single  
> tag  
> @resulttag.entries.collect(&:entries) will give you an array  
> containing an array for each tag, or if you want an array with all  
> entries,  
> @resulttag.entries.collect(&:entries).flatten will do the job
> 
> You could also approach it the other way round:
> 
> Entry.find\_by\_sql \<\<\_SQL  
> SELECT \* from entries  
> INNER JOIN entries\_tags on entries.id = entry\_id  
> INNER JOIN tags on tags.id = tag\_id  
> where name = ‘g’  
> \_SQL
> 
> Fred
