Hi All, I have a doubt regarding join tables I''m having 2 models 1)Fac 2)Cont and both models have " has and belong to many" relationships so there are 3 tables 1)facs 2)conts 3)conts_facs then i''m fetching the data in controller as @conts=Cont.find(:all]) @cfacs=Fac.all(:joins=>:conts, :select=>"facs.name") but i dont know how to get the cont_id in the join table from @cfacs example: for nd in @cfacs puts nd.cont_id end show me an error as "no method cont.id in Fac" Please help me regarding this
On Aug 8, 2009, at 2:16 AM, Kart wrote:> > Hi All, > I have a doubt regarding join tables > I''m having 2 models > 1)Fac > 2)Cont > > and both models have " has and belong to many" relationships > so there are 3 tables > 1)facs > 2)conts > 3)conts_facs > > then i''m fetching the data in controller as > @conts=Cont.find(:all]) > @cfacs=Fac.all(:joins=>:conts, :select=>"facs.name") > > but i dont know how to get the cont_id in the join table from @cfacs > example: > > for nd in @cfacs > puts nd.cont_id > end > > show me an error as "no method cont.id in Fac"Right. You told Rails only to select facs.name. It''s not clear from your post why you are using a :joins in a habtm association, as that should be implied, however something like this should work: @conts = Cont.all # assumes relatively recent Rails. If older, use Cont.find(:all) @conts.facs.each do |fac| puts fac[:id] end
thankyou S.Ross the problem is I have to get cont.id associated with each fac (foreign key)and in the @cfacs=Fac.all(:joins=>:conts, :select=>"facs.name") i''ll rewrite it as @cfacs=Fac.all(:joins=>:conts, :select=>"facs.*,facs_conts.*,conts.*<http://facs.name/> ") On Sun, Aug 9, 2009 at 1:11 AM, s.ross <cwdinfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Aug 8, 2009, at 2:16 AM, Kart wrote: > > > > > Hi All, > > I have a doubt regarding join tables > > I''m having 2 models > > 1)Fac > > 2)Cont > > > > and both models have " has and belong to many" relationships > > so there are 3 tables > > 1)facs > > 2)conts > > 3)conts_facs > > > > then i''m fetching the data in controller as > > @conts=Cont.find(:all]) > > @cfacs=Fac.all(:joins=>:conts, :select=>"facs.name") > > > > but i dont know how to get the cont_id in the join table from @cfacs > > example: > > > > for nd in @cfacs > > puts nd.cont_id > > end > > > > show me an error as "no method cont.id in Fac" > > Right. You told Rails only to select facs.name. It''s not clear from > your post why you are using a :joins in a habtm association, as that > should be implied, however something like this should work: > > @conts = Cont.all # assumes relatively recent Rails. If older, use > Cont.find(:all) > @conts.facs.each do |fac| > puts fac[:id] > end > > > > >-- Regards, Wasim +91-9003652450 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Don''t do @cfacs=Fac.all (:joins=>:conts, :select=>"facs.*,facs_conts.*,conts.*") If you need only the conts.id along with any other data that you need, do @cfacs = Fac.all(:include => :conts, :select=> "conts.id, facs.*") You don''t need to specify the join as the relationship takes care of it. On Aug 10, 9:18 am, wasim akram <akramwasim1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> thankyou S.Ross > > the problem is I have to get cont.id associated with each fac (foreign > key)and in the @cfacs=Fac.all(:joins=>:conts, :select=>"facs.name") i''ll > rewrite it as > @cfacs=Fac.all(:joins=>:conts, > :select=>"facs.*,facs_conts.*,conts.*<http://facs.name/> > ") > > > > > > On Sun, Aug 9, 2009 at 1:11 AM, s.ross <cwdi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Aug 8, 2009, at 2:16 AM, Kart wrote: > > > > Hi All, > > > I have a doubt regarding join tables > > > I''m having 2 models > > > 1)Fac > > > 2)Cont > > > > and both models have " has and belong to many" relationships > > > so there are 3 tables > > > 1)facs > > > 2)conts > > > 3)conts_facs > > > > then i''m fetching the data in controller as > > > @conts=Cont.find(:all]) > > > @cfacs=Fac.all(:joins=>:conts, :select=>"facs.name") > > > > but i dont know how to get the cont_id in the join table from @cfacs > > > example: > > > > for nd in @cfacs > > > puts nd.cont_id > > > end > > > > show me an error as "no method cont.id in Fac" > > > Right. You told Rails only to select facs.name. It''s not clear from > > your post why you are using a :joins in a habtm association, as that > > should be implied, however something like this should work: > > > @conts = Cont.all # assumes relatively recent Rails. If older, use > > Cont.find(:all) > > @conts.facs.each do |fac| > > puts fac[:id] > > end > > -- > Regards, > Wasim > +91-9003652450
thankyou Mukund i got it and it works On Mon, Aug 10, 2009 at 12:33 PM, Mukund <maruthim-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > Don''t do @cfacs=Fac.all > (:joins=>:conts, :select=>"facs.*,facs_conts.*,conts.*") > > If you need only the conts.id along with any other data that you need, > do > > @cfacs = Fac.all(:include => :conts, :select=> "conts.id, facs.*") > > You don''t need to specify the join as the relationship takes care of > it. > > On Aug 10, 9:18 am, wasim akram <akramwasim1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > thankyou S.Ross > > > > the problem is I have to get cont.id associated with each fac (foreign > > key)and in the @cfacs=Fac.all(:joins=>:conts, :select=>"facs.name") i''ll > > rewrite it as > > @cfacs=Fac.all(:joins=>:conts, > > :select=>"facs.*,facs_conts.*,conts.*<http://facs.name/> > > ") > > > > > > > > > > > > On Sun, Aug 9, 2009 at 1:11 AM, s.ross <cwdi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Aug 8, 2009, at 2:16 AM, Kart wrote: > > > > > > Hi All, > > > > I have a doubt regarding join tables > > > > I''m having 2 models > > > > 1)Fac > > > > 2)Cont > > > > > > and both models have " has and belong to many" relationships > > > > so there are 3 tables > > > > 1)facs > > > > 2)conts > > > > 3)conts_facs > > > > > > then i''m fetching the data in controller as > > > > @conts=Cont.find(:all]) > > > > @cfacs=Fac.all(:joins=>:conts, :select=>"facs.name") > > > > > > but i dont know how to get the cont_id in the join table from @cfacs > > > > example: > > > > > > for nd in @cfacs > > > > puts nd.cont_id > > > > end > > > > > > show me an error as "no method cont.id in Fac" > > > > > Right. You told Rails only to select facs.name. It''s not clear from > > > your post why you are using a :joins in a habtm association, as that > > > should be implied, however something like this should work: > > > > > @conts = Cont.all # assumes relatively recent Rails. If older, use > > > Cont.find(:all) > > > @conts.facs.each do |fac| > > > puts fac[:id] > > > end > > > > -- > > Regards, > > Wasim > > +91-9003652450 > > >-- Regards, Wasim +91-9003652450 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---