Do I create one big left join and then try to loop through fors blog
posts
and then comments, checking if the comment belongs to the post?
That's pretty much how I've done similar things with a left join.
Order the query by post id or post date or however you sequence them.
Assuming it's post id, you could loop through the rows in the returned
set something like this:
old_id = ''
for row in rows:
if old_id != row.posts.id:
# render row as a post
else:
# render row as a comment
old_id = row.posts.id
On Jul 27, 1:21 pm, pbreit <[email protected]> wrote:
> This brings up a question that I've had for awhile. If one page I want to
> show multiple blog posts each which has multiple comments, what is the best
> approach?
>
> Do I create one big left join and then try to loop through fors blog posts
> and then comments, checking if the comment belongs to the post?
>
> Or is there some way to run two queries, one each for posts and comments and
> then somehow mash them together in the loop?
>
> Something like this (not tested, no idea if it works)?
>
> posts = db(db.post.id>0)(db.comment.post==db.post.id).select()
> return dict(posts=posts)
>
> {{for row in rows:}}
> <h2>{{=row.posts.title}}</h2>
> <ol>
> {{for row in rows:}}
> <li>{{=row.comment.body if row.comment.post==row.post.id}}</li>
> {{pass}}
> </ol>
> {{pass}}
>
> That seems so inefficient running through every row for each post.
>
> Freak, you have to start simply, get something working and then add the
> complexity.