SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue with selecting items based on category id

    So I have a Categories table with top level categories and sub categories that have a parent_id column to associate the sub category to the top level.

    I then have an Items table with a category_id that associates the item to the category. The category_id always points to a sub category.

    How would I get all the items that are under a top level category?

    As an example, say I have the following category setup:

    Photoshop
    --Text Effects
    --Basics

    All of the items point to the category id's of those sub categories, but I'm wanting to get all the items under "Photoshop".

  2. #2
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming you don't have sub-sub-categories, something very close to this should work:
    Code Ruby:
    parent_id = params[:category_id]
    @items = Items.find(:all, :conditions => ['category_id in (select id from category where parent_id = ?)', parent_id])


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •