Best way to design uknown depth of subcategories


What would be the best way to design a table of posts that belong to a
unknown depth of categories. Say i wanted to display the posts of data
craigslist) but the category could be something like
NewYork/RealEstate/Housing/ForSale/ByOwner. Would acts_as_tree work
here and assign each sublevel to a different id? just trying to take
easiest approach here using the best tools. Also an easy way to display
category should be easy to accomplish as well with this model.


To me this sounds like something acts_as_tree is ideal for. Perhaps
something like:

class Category < AR::Base
has_many :posts

class Post < AR::Base
belongs_to :category

As for display… It all depends on what sort if UI you’re looking
for. You could easily flatten the category list and use a select box
to pick what a post belongs to. Alternatively you could display it as
a tree with links for each category which update either a visible or
hidden select via JavaScript. To do this you’d simply select
Categories with no parent and descend in a depth first manner (so
your constructed set of links looks like a proper tree rather than
jumping all over). Your links could be something along the lines of
(although ideally prettier):

<% onchange = “$(‘hiddenFieldId’).value = #{}” %>
<%= link_to “#”,, :onChange => onchange %>

Perhaps with some appropriate styling and classes applied to each
level of links to provide that tree-like depth (nested divs with a
padding-left style attribute would probably work nicely). Hope this

– Jon