jhaagmans wrote:
> Hi,
> 
> I have a model called "ProductAttribute":
> 
> class ProductAttribute < ActiveRecord::Base
>   belongs_to :product
>   has_many :children,  :foreign_key => :parent_id, :class_name =>
> 'ProductAttribute', :dependent => :destroy, :order => :position
>   belongs_to :parent, :class_name => 'ProductAttribute' if :parent_id
>   acts_as_list :scope => :parent
> end
> 
> As you can see, my goal is to have an attribute with a number of
> children and a parent, as long as it's not a root attribute. I think I
> got it right, but if you see any errors in this, please do tell me.

I think you did that part right.

> 
> Now I want to list an entire product, with all its attributes, in a
> view, which is where I got stuck. I want these associations to show
> hierarchically, so that a child is in fact a child element ( for
> example a list item ) of its parent. I can get all attributes using
> the product_id, so I have defined a collection called @attributes in
> my controller. But how to proceed in the view?

Use awesome_nested_set for this.  It's much better for arbitrary tree 
structures than the simple adjacency list that you're currently using.

> 
> My guess is that I need a partial which loads itself, but how to pass
> along the subcollection (a child -can- have another child which can
> have. another child and so on). 

With a nested-set model such as I am recommending, you can get all the 
descendants with a single query.  Without it...well...good luck. :)

> It's important that it sorts every
> subcollection (e.g. a few children of a certain child) by position: do
> I have that covered in my model by :order => :position?

Maybe.  Or you could use acts_as_list.

> 
> I really hope you can help.
> 
> Thank you!
> 
> Jaap Haagmans


Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to