To me this sounds like something acts_as_tree is ideal for. Perhaps
something like:
class Category < AR::Base
acts_as_tree
has_many :posts
end
class Post < AR::Base
belongs_to :category
end
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 =
#{cat.id}" %>
<%= link_to "#", cat.name, :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
helps.
-- Jon
On Dec 23, 2005, at 10:56 PM, Adam Denenberg wrote:
> Hello,
>
> 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 (i.e., craigslist) but the category could be
> something like NewYork/RealEstate/Housing/ForSale/ByOwner. Would
> acts_as_tree work best here and assign each sublevel to a different
> id? just trying to take the easiest approach here using the best
> tools. Also an easy way to display the category should be easy to
> accomplish as well with this model.
>
> thanks
> adam
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails