awesome_nested_set select statement

i’m trying to create an sql select from listings like this @listings=Listing.where("category_id in [1, 2, etc.])
i’m using awesome_nested_set and it has a “children” method helper for the Category model that returns all the children categories in an array I was thinking i have to step through the array to build a string to insert into my sql select statement, but i don’t wanna re-invent the wheel Since this is so common maybe there’s a method already written for this?

As I said in a previous post, if you are explicitly using model.id you
are probably not doing things the best way. Please provide details of
why you need to do this. There is almost certainly a better way.

Colin

i wanna display listings for that current category and all it’s child categories (if any) and in the sidebar i wanna display the child categories

As I said in a previous post, if you are explicitly using model.id you

are probably not doing things the best way. Please provide details of

why you need to do this. There is almost certainly a better way.

Let me be more clear I wanna display listings in leaf categories beneath the current category and in the sidebar all immediate child categories Of course, listings have to be in leaf categories only Thanks

i wanna display listings for that current category and all it's child
categories (if any) and in the sidebar i wanna display the child categories

But why are you having to reference the id? What are you trying to
get with the query you showed?

Colin

What is the relationship between listing and category?
In what way is a listing "in" a category.
What is a leaf category?
What do you mean by listings in leaf category?

Colin

But why are you having to reference the id? What are you trying to

get with the query you showed?

Colin

Listings will be shown not for all categories but for only some So if not by id how shall these categories be identified

I will wait for a reply to my previous email before attempting to answer this.

Colin

What is the relationship between listing and category?

In what way is a listing “in” a category.

What is a leaf category?

What do you mean by listings in leaf category?

Colin

categories has_many listings and listings belongs_to categories
a listing is in a category because the listing has a category_id field
leaf category is a category that has no children categories
listings can only be placed in leaf categories but can be seen when browsing it’s parent categories So if someone’s selling a toaster oven, they had to place the listing in Household->Kitchen->Small appliances->Toaster ovens but as soon as a user navigates only as far as Household they’ll see lisings including the toaster ovens and in the side pane should be links to all the categories beneath whatever category the user is currently in

Right, now we are getting somewhere.
So you want all the descendant categories of the current category
(children, grand children etc), and all the listings belonging to all
those categories. Is that right?

Colin

I’m sorry, i meant to say in the side pane should be links to only the immediate children categories, not all descendants, just the next level

so yes, that’s right

So in the side pane
@category.children.each do |category|
  category.name (or whatever)
end

and if in the main pane you want the listings for all descendant children
@category.descendants.each do |category|
  category.listings.each do |listing|
    # show listing data
  end
end

Colin

nice, thanks colin

doesn’t leave much for the controller … just set category?

doesn't leave much for the controller ... just set category?

If that is all you need in the view then yes. You could instead put
in the controller something like

@listings = []
@category.each do |category|
  @listings += category.listings
end

but I don't see the point. All you would be avoiding is the outer
level of 'each' loop in the view, and if anything it makes the code
less easy to understand.

There is likely a way of coding a query as you originally suggested,
and if there were going to be tens of thousands of categories then it
might even be worth it, but I always prefer the KISS principle until
it is proven that any benefit is worth the brain strain involved in
working it out. Life is too short.

Colin

thanks

This fails to display the listings

In my store_controller.rb
def catalog
@category=Category.find(params[:id])
end

In catalog.html.erb

<% @category.descendants.each do |category| %> <% category.listings.each do |listing| %> <%= listing.title %> <% end %> <% end %>

Colin thanks I’m gonna re-post my last post in this topic as a new topic so you can help me there ha ha

This fails to display the listings

In my store_controller.rb
  def catalog
   @category=Category.find(params[:id])
  end

In catalog.html.erb
<div>
<table>
<% @category.descendants.each do |category| %>
  <% category.listings.each do |listing| %>
   <%= listing.title %>
  <% end %>
<% end %>
</table>
</div>

I don't know whether it is the main problem but some tr and td
elements might help :slight_smile:

Colin