Dan Tenenbaum
2006-Mar-10 20:55 UTC
[Rails] problem when looping through habtm children in a view
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. -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Mar-11 03:41 UTC
[Rails] Re: problem when looping through habtm children in a view
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.
Dan Tenenbaum
2006-Mar-11 08:34 UTC
[Rails] Re: problem when looping through habtm children in a view
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. -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Mar-11 09:19 UTC
[Rails] Re: problem when looping through habtm children in a view
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.
Dan Tenenbaum
2006-Mar-11 19:50 UTC
[Rails] Re: problem when looping through habtm children in a view
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? -- Posted via http://www.ruby-forum.com/.