awesome_nested_set select statement

71 views
Skip to first unread message

fugee ohu

unread,
Nov 25, 2015, 7:31:31 AM11/25/15
to Ruby on Rails: Talk
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?

Colin Law

unread,
Nov 25, 2015, 8:39:05 AM11/25/15
to Ruby on Rails: Talk
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

fugee ohu

unread,
Nov 25, 2015, 10:50:35 AM11/25/15
to Ruby on Rails: Talk
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

fugee ohu

unread,
Nov 25, 2015, 11:01:03 AM11/25/15
to Ruby on Rails: Talk

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

 

Colin Law

unread,
Nov 25, 2015, 11:05:08 AM11/25/15
to Ruby on Rails: Talk
On 25 November 2015 at 15:50, fugee ohu <fuge...@gmail.com> wrote:
> 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

>
> On Wednesday, November 25, 2015 at 8:39:05 AM UTC-5, Colin Law wrote:
>>
>> On 25 November 2015 at 12:31, fugee ohu <fuge...@gmail.com> wrote:
>> > 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
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/562acce2-65e9-4c45-a376-03c14ccc7835%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Nov 25, 2015, 11:10:01 AM11/25/15
to Ruby on Rails: Talk
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

fugee ohu

unread,
Nov 25, 2015, 11:10:28 AM11/25/15
to Ruby on Rails: Talk
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

Colin Law

unread,
Nov 25, 2015, 11:14:20 AM11/25/15
to Ruby on Rails: Talk
I will wait for a reply to my previous email before attempting to answer this.

Colin

fugee ohu

unread,
Nov 25, 2015, 11:25:44 AM11/25/15
to Ruby on Rails: Talk
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

Colin Law

unread,
Nov 25, 2015, 11:33:48 AM11/25/15
to Ruby on Rails: Talk
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

fugee ohu

unread,
Nov 25, 2015, 11:46:20 AM11/25/15
to Ruby on Rails: Talk
 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

fugee ohu

unread,
Nov 25, 2015, 11:52:40 AM11/25/15
to Ruby on Rails: Talk

so yes, that's right

Colin Law

unread,
Nov 25, 2015, 11:56:06 AM11/25/15
to Ruby on Rails: Talk
On 25 November 2015 at 16:46, fugee ohu <fuge...@gmail.com> wrote:
> 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 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

fugee ohu

unread,
Nov 25, 2015, 12:02:44 PM11/25/15
to Ruby on Rails: Talk
nice, thanks colin

fugee ohu

unread,
Nov 25, 2015, 12:55:57 PM11/25/15
to Ruby on Rails: Talk
doesn't leave much for the controller ... just set category?


On Wednesday, November 25, 2015 at 11:56:06 AM UTC-5, Colin Law wrote:

Colin Law

unread,
Nov 25, 2015, 1:28:00 PM11/25/15
to Ruby on Rails: Talk
On 25 November 2015 at 17:55, fugee ohu <fuge...@gmail.com> wrote:
> 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
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/a0f9a98b-28e2-4e7d-8f5e-2e4b5f863f8b%40googlegroups.com.

fugee ohu

unread,
Nov 25, 2015, 3:15:27 PM11/25/15
to Ruby on Rails: Talk
thanks

fugee ohu

unread,
Nov 25, 2015, 4:27:42 PM11/25/15
to Ruby on Rails: Talk
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>


fugee ohu

unread,
Nov 25, 2015, 4:47:44 PM11/25/15
to Ruby on Rails: Talk
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

Colin Law

unread,
Nov 25, 2015, 4:57:00 PM11/25/15
to Ruby on Rails: Talk
I don't know whether it is the main problem but some tr and td
elements might help :)

Colin

>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/be360451-a042-4b5f-96c1-f64c707e2cbe%40googlegroups.com.

fugee ohu

unread,
Nov 25, 2015, 5:08:51 PM11/25/15
to Ruby on Rails: Talk
yikes! but nope, it still renders a blank page, i typed some text to display just to make sure i was rendering the right file and that's confirmed

fugee ohu

unread,
Nov 26, 2015, 4:15:31 PM11/26/15
to Ruby on Rails: Talk
Works good now Thanks
Reply all
Reply to author
Forward
0 new messages