# About joinTable

**URL:** <https://www.ruby-forum.com/t/about-jointable/173277>\
**Category:** Rails\
**Created:** [August 6, 2009, 6:31am UTC](https://www.ruby-forum.com/t/about-jointable/173277 "2009-08-06T06:31:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 6, 2009, 6:31am UTC](https://www.ruby-forum.com/t/about-jointable/173277/1 "2009-08-06T06:31:57Z")

</div>

Hi

Below

Table1: name\_table

id name  
1 name1  
2 name2

Table2: address\_table

id address  
1 address1  
2 address2

jointable : name\_address

name\_id address\_id  
1 1  
2 2

i dont know how to get all details , as i very new to this ruby on rails  
whether to access name\_address table to get all details

My output should be

name1 address1  
name2 address2

please guide me

–  
Karthik.k  
Mobile - +91-9894991640

---

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 6, 2009, 6:52am UTC](https://www.ruby-forum.com/t/about-jointable/173277/2 "2009-08-06T06:52:02Z")

</div>

Hi

Assume tables are names, addresses  
So the junction table should be addresses\_names by default This  
can be override if needed… And the join table wont have id column This  
is has\_and\_belongs\_to\_many(HABTM)  
Have you setup relationships properly If so you can get all the  
addresses corresponding to a name by

example  
name = Name.first  
name.addresses

and viceversa

address = Address.find 1  
address.names

And better rename your tables to something like contacts and  
contact\_addresses

Sijo

---

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 6, 2009, 7:12am UTC](https://www.ruby-forum.com/t/about-jointable/173277/3 "2009-08-06T07:12:20Z")

</div>

On Thu, Aug 6, 2009 at 10:22 AM, Sijo Kg  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> example
> 
> Sijo
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Hi Sijo

thank you

I will explain the real need

Table 1: agencie

id name description  
1 agency1 desc1  
2 agency2 desc2

Table 2: contract

id name description  
1 contract1 desc1  
2 contract2 desc2

Table 3 : agency\_contracts

agency\_id contract\_id  
1 1  
2 1  
1 2  
2 2

So form the above table you can find one contract can contain any number  
or  
gagencies

so i need to display as  
contract1  
agency1  
agency2  
contract2  
agency1  
agency2

I think you can under stand inside one contract two agency comes in as  
it is  
in agency\_contracts table

I need to write a ror format query and not in sql query(As my client  
needs)

this is what i need sijo

plz help

–  
Karthik.k  
Mobile - +91-9894991640

---

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 6, 2009, 7:19am UTC](https://www.ruby-forum.com/t/about-jointable/173277/4 "2009-08-06T07:19:21Z")

</div>

On Thu, Aug 6, 2009 at 10:43 AM, Sniper A. \<  
removed\_email\_address@domain.invalid\> wrote:

> > 2 name2  
> > 1 1  
> > please guide me  
> > has\_many + :through
> 
> >

hi Blade

thank you

I have explained my exact requirement in my previous mail

I need a query which will take data from join table and display

though i dont know about that but saw in blog like

e.g

@agencies=Agenci.all(:joins …)

though i dont know this

Please guide me

–  
Karthik.k  
Mobile - +91-9894991640

---

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 6, 2009, 7:28am UTC](https://www.ruby-forum.com/t/about-jointable/173277/5 "2009-08-06T07:28:00Z")

</div>

Karthik Kantharaj wrote:

> On Thu, Aug 6, 2009 at 10:43 AM, Sniper A. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > > 2 name2  
> > > 1 1  
> > > please guide me  
> > > has\_many + :through
> > 
> > >
> 
> hi Blade
> 
> thank you
> 
> I have explained my exact requirement in my previous mail
> 
> I need a query which will take data from join table and display
> 
> though i dont know about that but saw in blog like
> 
> e.g
> 
> @agencies=Agenci.all(:joins …)
> 
> though i dont know this
> 
> Please guide me
> 
> –  
> Karthik.k  
> Mobile - +91-9894991640

rename you tables like below

Table 1 : agencies  
Table 2 : contracts  
Table 3 : agencies\_contracts

then these are the model

class Agency \< AR::Base  
has\_and\_belongs\_to\_many :contracts  
end

class Contract \< AR::Base  
has\_and\_belongs\_to\_many :agencies  
end

Agency.find(id).contracts #=\> […]  
Contract.find(id).agencies #=\> […]

---

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 6, 2009, 7:13am UTC](https://www.ruby-forum.com/t/about-jointable/173277/6 "2009-08-06T07:13:56Z")

</div>

Karthik Kantharaj wrote:

> Hi
> 
> Below
> 
> Table1: name\_table
> 
> id name  
> 1 name1  
> 2 name2
> 
> Table2: address\_table
> 
> id address  
> 1 address1  
> 2 address2
> 
> jointable : name\_address
> 
> name\_id address\_id  
> 1 1  
> 2 2
> 
> i dont know how to get all details , as i very new to this ruby on rails  
> whether to access name\_address table to get all details
> 
> My output should be
> 
> name1 address1  
> name2 address2
> 
> please guide me
> 
> –  
> Karthik.k  
> Mobile - +91-9894991640

if you want only many-many relation then use habtm

otherwise better try to use

has\_many + :through

class Name \< AR::Base  
has\_many :addresses\_names  
has\_many :addresses ,:through=\>:addresses\_names  
end

class Address \< AR::base  
has\_many :addresses\_names  
has\_many :names ,:through=\>:addresses\_names  
end

class AddressesName \< AR::Base  
belongs\_to :address  
belongs\_to :name  
end

> Blade  
> Mobile - +91-9916237431

---

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 6, 2009, 10:23am UTC](https://www.ruby-forum.com/t/about-jointable/173277/7 "2009-08-06T10:23:43Z")

</div>

On Thu, Aug 6, 2009 at 10:58 AM, Sniper A. \<  
removed\_email\_address@domain.invalid\> wrote:

> > > >
> > 
> > Mobile - +91-9894991640
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> > 
> 
> Hi Sniper

Thank you

–  
Karthik.k  
Mobile - +91-9894991640
