About joinTable

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

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

On Thu, Aug 6, 2009 at 10:22 AM, Sijo Kg
[email protected]wrote:

example

Sijo


Posted via 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

On Thu, Aug 6, 2009 at 10:43 AM, Sniper A. <
[email protected]> 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

Karthik Kantharaj wrote:

On Thu, Aug 6, 2009 at 10:43 AM, Sniper A. <
[email protected]> 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 #=> […]

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

On Thu, Aug 6, 2009 at 10:58 AM, Sniper A. <
[email protected]> wrote:

Mobile - +91-9894991640


Posted via http://www.ruby-forum.com/.

Hi Sniper

Thank you


Karthik.k
Mobile - +91-9894991640