# Acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/acts-as-tree/108255>\
**Category:** Rails\
**Created:** [July 1, 2007, 2:54am UTC](https://www.ruby-forum.com/t/acts-as-tree/108255 "2007-07-01T02:54:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [July 1, 2007, 2:54am UTC](https://www.ruby-forum.com/t/acts-as-tree/108255/1 "2007-07-01T02:54:30Z")

</div>

I’m new to rails and was playing with a simple model with  
acts\_as\_threaded. The sql is…

CREATE TABLE bobbits  
(  
id integer NOT NULL DEFAULT  
nextval(‘destination\_countrycode\_id\_seq’::regclass),  
name character varying(100) NOT NULL,  
description text,  
parent\_id integer,  
CONSTRAINT bobbits\_pkey PRIMARY KEY (id)  
)

and the only change i made after generating scaffolding was of course:

class Bobbit \< ActiveRecord::Base  
acts\_as\_tree :order =\> “name”  
end

Now I expected that rendering of a bobbit in the browser would present  
an drop-down list for parent, but no such luck. Whats up?

---

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [July 1, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/acts-as-tree/108255/2 "2007-07-01T13:14:12Z")

</div>

On Jun 30, 8:53 pm, dailer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> CONSTRAINT bobbits\_pkey PRIMARY KEY (id)  
> )
> 
> and the only change i made after generating scaffolding was of course:
> 
> class Bobbit \< ActiveRecord::Base  
> acts\_as\_tree :order =\> “name”  
> end
> 
> Now I expected that rendering of a bobbit in the browser would present  
> an drop-down list for parent, but no such luck. Whats up?

could someone at least confirm that I should see a dropdown?

---

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [July 1, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/acts-as-tree/108255/3 "2007-07-01T13:22:02Z")

</div>

dailer wrote:

> On Jun 30, 8:53 pm, dailer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > CONSTRAINT bobbits\_pkey PRIMARY KEY (id)  
> > )
> > 
> > and the only change i made after generating scaffolding was of course:

> could someone at least confirm that I should see a dropdown?

i don’t know of a dropdown that should be populated automatically with  
“generate scaffold”, but i haven’t used the ‘generate scaffold’ in a  
long time, so i may be way outdated. to answer your question though, i  
would have to say i personally don’t know of any dropdown that should be  
generated when creating an association.

---

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [July 1, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/acts-as-tree/108255/4 "2007-07-01T13:23:22Z")

</div>

> generated when creating an association.

… or acts\_as\_tree etc

---

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [July 1, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/acts-as-tree/108255/5 "2007-07-01T13:31:14Z")

</div>

On Jun 30, 8:53 pm, dailer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> CONSTRAINT bobbits\_pkey PRIMARY KEY (id)  
> )
> 
> and the only change i made after generating scaffolding was of course:
> 
> class Bobbit \< ActiveRecord::Base  
> acts\_as\_tree :order =\> “name”  
> end
> 
> Now I expected that rendering of a bobbit in the browser would present  
> an drop-down list for parent, but no such luck. Whats up?

could someone at least confirm that I should see a dropdown?

---

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [July 1, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/acts-as-tree/108255/6 "2007-07-01T13:48:16Z")

</div>

On Jul 1, 7:23 am, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > generated when creating an association.
> 
> … or acts\_as\_tree etc
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

ah, I see my mistake was skimming the recipe example and I missed the  
part where the view was updated with the select/dropdown.

---

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [July 2, 2007, 3:14am UTC](https://www.ruby-forum.com/t/acts-as-tree/108255/7 "2007-07-02T03:14:36Z")

</div>

No. acts\_as\_tree is an extension to ActiveRecord – the objects-  
wrapping-db mechanism in rails (Model). Dropdowns are a function of  
the view. By default, the scaffold will generate a text\_column for  
almost everything but date, datetime, and boolean fields.

If you want a dropdown for parent, you need to change the scaffolded  
field that probably looks like:

**Parent** \<%= text\_field f.parent\_id %\> \<----- this one

To something along the lines of…

**Parent** \<%= select f.parent\_id, Bobbit.find(:all, :order=\>'name').collect{| bobbit| [bobbit.name, bobbit.id]} %\>

The select object accepts the name of the attribute to be filled  
(parent\_id), and an enumerable set of options. In the example above,  
you’re retrieving all the Bobbits, ordered by their name, and creating  
an array of name/val pairs to be displayed in the dropdown.

HTH,  
AndyV
