Forum: Ruby on Rails problem when looping through habtm children in a view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1f1bd7de8337317975b0475be56392d2?d=identicon&s=25 Dan Tenenbaum (dandante)
on 2006-03-10 21:55
I have this code in a view:

<% for topic in @topics %>
    <%= topic.id %> <%= topic.dr_title %><br/>
        <ul>
        <% for doc in topic.child_documents %>
             <%= doc.dr_title %><br/>

             <% 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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-11 04:42
(Received via mailing list)
Dan Tenenbaum 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.
1f1bd7de8337317975b0475be56392d2?d=identicon&s=25 Dan Tenenbaum (dandante)
on 2006-03-11 09:34
Mark Reginald James 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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-11 10:20
(Received via mailing list)
Dan Tenenbaum wrote:
> Mark Reginald James 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.
1f1bd7de8337317975b0475be56392d2?d=identicon&s=25 Dan Tenenbaum (dandante)
on 2006-03-11 20:50
Mark Reginald James 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?
This topic is locked and can not be replied to.