habtm joining and getting data problem

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

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

hello Rahul,

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

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

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