# Problem when looping through habtm children in a view

**URL:** <https://www.ruby-forum.com/t/problem-when-looping-through-habtm-children-in-a-view/79663>\
**Category:** Rails\
**Created:** [March 10, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/problem-when-looping-through-habtm-children-in-a-view/79663 "2006-03-10T21:55:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [March 10, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/problem-when-looping-through-habtm-children-in-a-view/79663/1 "2006-03-10T21:55:52Z")

</div>

I have this code in a view:

\<% for topic in @topics %\>  
\<%= topic.id %\> \<%= topic.dr\_title %\>

  
\<% for doc in topic.child\_documents %\>  
\<%= doc.dr\_title %\>  

```
         <% doc = nil %>
    <% end %>
    </ul>

```

\<% end %\>

Though the topic titles and IDs display correctly, the child documents  
for every topic are the same (the ones belonging to the first topic). In  
other words, I expect an SQL query to be run each time through the inner  
loop, and it is, but it is always using the same topic id (that of the  
first topic in the loop).

The habtm for child\_documents defined in my model uses a custom  
finder\_sql which uses the id variable for the topic id. Could this  
somehow not be populated correctly?

I can make the code work using other methods, but I’m wondering why this  
fails. Thanks.

---

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [March 11, 2006, 4:42am UTC](https://www.ruby-forum.com/t/problem-when-looping-through-habtm-children-in-a-view/79663/2 "2006-03-11T04:42:03Z")

</div>

Dan T. wrote:

> Though the topic titles and IDs display correctly, the child documents  
> for every topic are the same (the ones belonging to the first topic). In  
> other words, I expect an SQL query to be run each time through the inner  
> loop, and it is, but it is always using the same topic id (that of the  
> first topic in the loop).
> 
> The habtm for child\_documents defined in my model uses a custom  
> finder\_sql which uses the id variable for the topic id. Could this  
> somehow not be populated correctly?

Is your finder\_sql correctly quoted to delay evaluation of #{id}?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [March 11, 2006, 9:34am UTC](https://www.ruby-forum.com/t/problem-when-looping-through-habtm-children-in-a-view/79663/3 "2006-03-11T09:34:21Z")

</div>

Mark Reginald J. wrote:

> Is your finder\_sql correctly quoted to delay evaluation of #{id}?

Can you give an example of how to quote it? Currently the finder sql is  
a single-quoted string  
and the ID is interpolated as follows: #{id}

I tried changing it to be a double quoted string and the topic id’s  
showed up as totally bogus very long numbers.

This is something that is not very well documented in the Rails book and  
api documentation.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [March 11, 2006, 10:20am UTC](https://www.ruby-forum.com/t/problem-when-looping-through-habtm-children-in-a-view/79663/4 "2006-03-11T10:20:10Z")

</div>

Dan T. wrote:

> Mark Reginald J. wrote:
> 
> > Is your finder\_sql correctly quoted to delay evaluation of #{id}?
> 
> Can you give an example of how to quote it? Currently the finder sql is  
> a single-quoted string  
> and the ID is interpolated as follows: #{id}
> 
> I tried changing it to be a double quoted string and the topic id’s  
> showed up as totally bogus very long numbers.

You’ve got the quoting right. Check your Rails logs to see  
if the right database queries are being sent.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [March 11, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/problem-when-looping-through-habtm-children-in-a-view/79663/5 "2006-03-11T20:50:36Z")

</div>

Mark Reginald J. wrote:

> You’ve got the quoting right. Check your Rails logs to see  
> if the right database queries are being sent.

The right queries are being sent, with the wrong value for id. I am  
looping through a set of topics and showing the child documents of each.  
The first topic in the loop has the id of 207. ALL the queries to  
retrieve child documents have the ID as 207.

I know the topics outer loop in my view is working correctly because it  
shows a different topic each time through.

Though I still want the answer to this, I worked around it by creating a  
get\_child\_documents method in the Topic model class, which is just a  
call to Topic.find\_by\_sql with the same exact query that is in the  
finder\_sql. So that raises the question, why does that work but not  
using the habtm to retrieve topic.child\_documents?
