Hi, I have a model "Theme" which has_and_belongs_to_many "Lessons". I need to get the (db) id of the lessons through the theme. For instance, if in irb I write something like this : test = Theme.find(:first) puts test.lessons.first.id I get the ID of test (the theme) and not of the lesson. Do you know how to get the id of the nested object instead ? Thanks Lily -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Maybe they have the same id :-), checkout your database and see if they have the same id 2010/7/21 Lily ^_^ <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Hi, > > I have a model "Theme" which has_and_belongs_to_many "Lessons". > > I need to get the (db) id of the lessons through the theme. > For instance, if in irb I write something like this : > > test = Theme.find(:first) > puts test.lessons.first.id > > I get the ID of test (the theme) and not of the lesson. > > Do you know how to get the id of the nested object instead ? > > Thanks > Lily > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sergio Sergio wrote:> Maybe they have the same id :-), checkout your database and see if they > have > the same id > > 2010/7/21 Lily ^_^ <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>Already checked :) The parent has ID 3 in its table. The lesson has ID 1 in its table. And thetheme.thelesson.id gives "3"... Well actually it''s in an XML builder, maybe you''ll find a mistake : xml.list do theme.lessons.each do |lesson| xml.item do xml.label lesson.title xml.link "/lessons/" + lesson.id.to_s + ".xml" end end end The way I''m writing xml.link is really not graceful, I admit. It''s the first time I need to do something such so if there''s a nicer way to do it, i would be glad to know. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
then, the lesson.id is incorrect but the lesson.title is ok ?... does your HABTM table contains just the theme_id and lesson_id fields ? 2010/7/21 Lily ^_^ <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Sergio Sergio wrote: > > Maybe they have the same id :-), checkout your database and see if they > > have > > the same id > > > > 2010/7/21 Lily ^_^ <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > > Already checked :) > The parent has ID 3 in its table. > The lesson has ID 1 in its table. > > And thetheme.thelesson.id gives "3"... > > Well actually it''s in an XML builder, maybe you''ll find a mistake : > > xml.list do > theme.lessons.each do |lesson| > xml.item do > xml.label lesson.title > xml.link "/lessons/" + lesson.id.to_s + ".xml" > end > end > end > > The way I''m writing xml.link is really not graceful, I admit. It''s the > first time I need to do something such so if there''s a nicer way to do > it, i would be glad to know. > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 21, 4:14 pm, Lily ^_^ <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > I have a model "Theme" which has_and_belongs_to_many "Lessons". > > I need to get the (db) id of the lessons through the theme. > For instance, if in irb I write something like this : > > test = Theme.find(:first) > puts test.lessons.first.id > > I get the ID of test (the theme) and not of the lesson. > > Do you know how to get the id of the nested object instead ? >if your join table has an id column weird stuff like this will happen Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Jul 21, 4:14�pm, Lily ^_^ <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I get the ID of test (the theme) and not of the lesson. >> >> Do you know how to get the id of the nested object instead ? >> > > if your join table has an id column weird stuff like this will happen > > FredThank you so much guys !!! I forgot to remove the id in the join table creation migration file. So the join table had an incremential id and lessons.id was actually the join table line id (which was by chance the same as theme.id)... confusing. I fixed it and it works perfect now. Thank you! I wouldn''t have thought about checking this :S Have a nice day Lily -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.