Question

I am writing a simple message board system. One thing about ancestry is that it uses a single call so that if I call with limit and offset, it will not return a consistent list of top level elements. I need the @per_page and the_offset for handling pagination.

So I currently have something like this:

@posts=MbPost.where('forum_id=?',params[:id]).offset(the_offset).limit(@per_page).order('updated_at desc')

but need the offset and @per_page to be reflective of the top-level (ie ancestry is NULL) not the total number of elements.

Any idea on how to do this?

thx in advance

Was it helpful?

Solution

You won't be able to do it with a single DB Query.

Reason is that with say pagination of 10, you want to show 10 top level posts(in actual say 15 posts) but in next page of it for 10 more top level posts you might be showing 20 actual posts.. So the pagination size is different.

The other way to handle that would be to have two query : one for actual page with : ancestry is null and applying limit and offset. The other query then to fetch all the posts which are the any-level child of posts you have in previous query..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top