Scott Messinger
Scott Messinger

Reputation: 159

Adding Children with awesome_nested_set in rails

How do I add children to a parent using the awesome_nested_set plugin?

I have a model, Unit, which is a nested set. I'd like to add sub-units. Within the edit view, how I let the user add children (sub-units) to the parent (unit)?

Upvotes: 0

Views: 932

Answers (2)

uomo_perfetto
uomo_perfetto

Reputation: 2414

On one model called Category... Try:

science = Category.where(:name => 'Science')
physics = Category.create!(:name => 'Physics')
physics.move_to_child_of(science)

This add category "Physics" to category "Science" i.e.:

   ■Science
    ╚ Physics 

Upvotes: 1

Joc
Joc

Reputation: 1059

I have been implementing something with this gem recently and here's how I approached it:

In the _form partial I used a collection_select with

<%= f.collection_select :parent_id, Unit.root.self_and_descendants, :id, :name %>

where 'f' is supplied by your form_for and it assumes that Unit has a field 'name' to display in the Select but you can change that as needed.

Upvotes: 2

Related Questions