# IDs problem

**URL:** <https://www.ruby-forum.com/t/ids-problem/191975>\
**Category:** Rails\
**Created:** [July 21, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/ids-problem/191975 "2010-07-21T17:14:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilyweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lilyweb](https://www.ruby-forum.com/u/lilyweb)\
**Post date:** [July 21, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/ids-problem/191975/1 "2010-07-21T17:14:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lilyweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lilyweb](https://www.ruby-forum.com/u/lilyweb)\
**Post date:** [July 21, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/ids-problem/191975/2 "2010-07-21T17:23:01Z")

</div>

Maybe they have the same id :-), checkout your database and see if they  
have  
the same id

2010/7/21 Lily 🙂 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lilyweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lilyweb](https://www.ruby-forum.com/u/lilyweb)\
**Post date:** [July 21, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/ids-problem/191975/3 "2010-07-21T17:29:41Z")

</div>

Sergio S. wrote:

> Maybe they have the same id :-), checkout your database and see if they  
> have  
> the same id
> 
> 2010/7/21 Lily 🙂 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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.

---

<div class="post-metadata">

**Author:** ![lilyweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lilyweb](https://www.ruby-forum.com/u/lilyweb)\
**Post date:** [July 21, 2010, 6:26pm UTC](https://www.ruby-forum.com/t/ids-problem/191975/4 "2010-07-21T18:26:15Z")

</div>

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 🙂 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lilyweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lilyweb](https://www.ruby-forum.com/u/lilyweb)\
**Post date:** [July 21, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/ids-problem/191975/5 "2010-07-21T18:28:36Z")

</div>

On Jul 21, 4:14 pm, Lily 🙂 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

Fred

---

<div class="post-metadata">

**Author:** ![lilyweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lilyweb](https://www.ruby-forum.com/u/lilyweb)\
**Post date:** [July 21, 2010, 6:40pm UTC](https://www.ruby-forum.com/t/ids-problem/191975/6 "2010-07-21T18:40:30Z")

</div>

Frederick C. wrote:

> On Jul 21, 4:14ï¿½pm, Lily 🙂 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
> 
> Fred

Thank 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
