# Habtm joining and getting data problem

**URL:** <https://www.ruby-forum.com/t/habtm-joining-and-getting-data-problem/187001>\
**Category:** Rails\
**Created:** [April 15, 2010, 3:00pm UTC](https://www.ruby-forum.com/t/habtm-joining-and-getting-data-problem/187001 "2010-04-15T15:00:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 15, 2010, 3:00pm UTC](https://www.ruby-forum.com/t/habtm-joining-and-getting-data-problem/187001/1 "2010-04-15T15:00:08Z")

</div>

i have two module both are joined by HABTM name are tags and  
content\_masters

i have content\_master .id and i want to get all the content\_masters  
which are with the same tag as that id has .

means if my content\_master .id=1 is associated with tag\_id 1,2  
than i want the listing of all the content\_master with tag\_id=1,2

please suggest how to do in rails

thanks

rahul

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 15, 2010, 3:17pm UTC](https://www.ruby-forum.com/t/habtm-joining-and-getting-data-problem/187001/2 "2010-04-15T15:17:41Z")

</div>

On 15 April 2010 13:58, Rahul M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i have two module both are joined by HABTM name are tags and  
> content\_masters
> 
> i have content\_master .id and i want to get all the content\_masters  
> which are with the same tag as that id has .
> 
> means if my content\_master .id=1 is associated with tag\_id 1,2  
> than i want the listing of all the content\_master with tag\_id=1,2

Do you really have just the id of the content master or do you have  
the object itself? If you have a content\_master in @content\_master  
then the associated tags are available as an array in  
@content\_master.tags.  
If you only have the id of the content master then something like  
@tags = Tag.find( :all, :include =\> ‘content\_masters’, :conditions =\>  
[‘content\_masters.id = ?’, content\_master.id] )

Colin

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 15, 2010, 6:18pm UTC](https://www.ruby-forum.com/t/habtm-joining-and-getting-data-problem/187001/3 "2010-04-15T18:18:02Z")

</div>

hello Rahul,

On Apr 15, 5:58 pm, Rahul M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i have two module both are joined by HABTM name are tags and  
> content\_masters

> i have content\_master .id and i want to get all the content\_masters  
> which are with the same tag as that id has .

if it is joined by has\_and\_belongs\_to\_many , then you can simply do  
this by  
@content\_master = ContentMaster.find(1) # id= 1 is just taken as  
example  
@content\_master.tag # this will give  
you array of all the tags that are assosiated to id 1

in the case you want reverse then

@tag = Tag.find(1) # assuming tag to be a  
model  
@tag.content\_master # this will give you array of  
all the content\_master assosiated with tag\_id = 1

hope this helps

mac

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 16, 2010, 6:59am UTC](https://www.ruby-forum.com/t/habtm-joining-and-getting-data-problem/187001/4 "2010-04-16T06:59:11Z")

</div>

Hii mac (manmohan ),

thanks for answer you have given me the method to find out to content  
master listing of tag id 1 but i want the content master listing of  
all which are associated with tag id 1,2

please help in this regard

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 16, 2010, 3:39pm UTC](https://www.ruby-forum.com/t/habtm-joining-and-getting-data-problem/187001/5 "2010-04-16T15:39:43Z")

</div>

have u tried using :

ContentMaster.find\_by\_sql(“Select \* From content\_master where tag\_id=1  
AND tag\_id=2”)

then follow the same steps for array and make loop .  
I suppose this should solve your problem

thank you,  
manmohan vyas
