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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/.
On Thu, Aug 6, 2009 at 10:22 AM, Sijo Kg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > 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 > > > -- > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-9894991640if 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-- Posted via http://www.ruby-forum.com/.
On Thu, Aug 6, 2009 at 10:43 AM, Sniper Abandon < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > -- > Posted via http://www.ruby-forum.com/. > > > >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Karthik Kantharaj wrote:> On Thu, Aug 6, 2009 at 10:43 AM, Sniper Abandon < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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-9894991640rename 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 #=> [...] -- Posted via http://www.ruby-forum.com/.
On Thu, Aug 6, 2009 at 10:58 AM, Sniper Abandon < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Karthik Kantharaj wrote: > > On Thu, Aug 6, 2009 at 10:43 AM, Sniper Abandon < > > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 #=> [...] > > > -- > Posted via http://www.ruby-forum.com/. > > > > Hi SniperThank you -- Karthik.k Mobile - +91-9894991640 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---