# Acts\_as\_threaded - help?

**URL:** <https://www.ruby-forum.com/t/acts-as-threaded-help/54912>\
**Category:** Rails\
**Created:** [March 15, 2006, 6:13am UTC](https://www.ruby-forum.com/t/acts-as-threaded-help/54912 "2006-03-15T06:13:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [March 15, 2006, 6:13am UTC](https://www.ruby-forum.com/t/acts-as-threaded-help/54912/1 "2006-03-15T06:13:50Z")

</div>

Hi, has anyone successfully used the acts\_as\_threaded plugin with  
postgresql?

I’m using rails 1.0 and ruby 1.8.4 on linux.

Following the screencast on  
[http://www.railtie.net/articles/2006/02/05/rails-acts\_as\_threaded-plugin](http://www.railtie.net/articles/2006/02/05/rails-acts_as_threaded-plugin)  
,  
I got to where we’re ready to create our first post, having made the  
changes to controllers/posts\_controller.rb, views/posts/\_form.rhtml,  
views/posts/show.rhtml, and models/post.rb as shown in the screencast.

When I submit the post I get an SQL error:  
PGError: ERROR: null value in column “root\_id” violates not-null  
constraint:  
INSERT INTO posts (“name”, “updated\_at”, “body”, “depth”, “lft”,  
“subject”, “root\_id”, “parent\_id”, “rgt”, “created\_at”)  
VALUES(‘Richard Nixon’, ‘2006-03-14 19:45:58’,  
‘Erase tape number 2347!’, 0, 0, ‘Memo’, NULL, NULL, 0,  
‘2006-03-14 19:43:00’)

We can see NULL values being provided for root\_id and parent\_id,  
and I believe this to be incorrect. (I tried removing the NOT NULL  
constraints, but got NoMethodError for nil exceptions further along.)  
Note: The depth, lft, and rgt fields would have been NULL also, but I  
set them to zero in the form manually before submitting the form.

This seems to be a key difference from the screencast. The  
screencast shows the form already displaying default values of  
zero for Depth, Lft, and Rgt:  
[http://tastyspleen.net/~billk/acts\_as\_threaded\_post\_defaults.png](http://tastyspleen.net/~billk/acts_as_threaded_post_defaults.png)

Whereas in my app, these fields are blank:  
[http://tastyspleen.net/~billk/acts\_as\_threaded\_post\_nodefaults.png](http://tastyspleen.net/~billk/acts_as_threaded_post_nodefaults.png)

Can anyone suggest any hints or ideas as to why the form in  
the screencast would have default values pre-populated for some fields,  
but my application presents the form with these fields uninitialized?

These are the files I’ve modified from the scaffold, with only the  
very slight changes shown in the screencast:

[http://cila-search.net/~billk/forum\_060314/app/models/post.rb](http://cila-search.net/~billk/forum_060314/app/models/post.rb)  
[http://cila-search.net/~billk/forum\_060314/app/controllers/posts\_controller.rb](http://cila-search.net/~billk/forum_060314/app/controllers/posts_controller.rb)  
[http://cila-search.net/~billk/forum\_060314/app/views/posts/\_form.rhtml](http://cila-search.net/~billk/forum_060314/app/views/posts/_form.rhtml)  
[http://cila-search.net/~billk/forum\_060314/app/views/posts/show.rhtml](http://cila-search.net/~billk/forum_060314/app/views/posts/show.rhtml)

Thanks for any help, hints, suggestions, possibilities, etc. !

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [March 15, 2006, 8:16am UTC](https://www.ruby-forum.com/t/acts-as-threaded-help/54912/2 "2006-03-15T08:16:33Z")

</div>

The fields are defined as default 0 in mysql.

Instead of using my acts\_as\_threaded plugin, try this code using the  
default acts\_as\_nested set act in AR.

Stick this code in you threaded/nested\_set model

### HANDLE ACTS AS NESTED SET FUNCTIONALITY

acts\_as\_nested\_set :scope =\> :root

def before\_create  
# Update the child object with its parents attrs  
unless self[:parent\_id].to\_i.zero?  
self[:depth] = parent[:depth] + 1  
self[:root\_id] = parent[:root\_id]  
end  
end

def after\_create  
# Update the parent root\_id with its id  
if self[:parent\_id].to\_i.zero?  
self[:root\_id] = self[:id]  
self.save  
else  
parent.add\_child self  
end  
end

def parent  
@parent ||= self.class.find(self[:parent\_id])  
end

And in your db definition:

root\_id int  
parent\_id int default 0  
lft int  
rgt int  
depth int default 0

When using it, just make sure child entries have their parent\_id set  
when saving and presto! everything works just like acts\_as\_threaded.

Good luck, I’ll blog about this at some point when my load lightens up.

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [March 15, 2006, 9:54am UTC](https://www.ruby-forum.com/t/acts-as-threaded-help/54912/3 "2006-03-15T09:54:06Z")

</div>

From: “Bob S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> The fields are defined as default 0 in mysql.
> 
> Instead of using my acts\_as\_threaded plugin, try this code using the  
> default acts\_as\_nested set act in AR.
> 
> Stick this code in you threaded/nested\_set model  
> […]

Hi Bob, - Thanks !!!

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [April 19, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/acts-as-threaded-help/54912/4 "2006-04-19T20:28:30Z")

</div>

Bob,

This lets you create a new child. How would you go about updating a  
child  
with a new parent?

Thanks

Dom

On 15 Mar 2006 07:13:18 -0000, Bob S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 3, 2006, 2:43am UTC](https://www.ruby-forum.com/t/acts-as-threaded-help/54912/5 "2006-05-03T02:43:33Z")

</div>

How can I “make sure child entries have their parent\_id set when  
saving?”  
Is this done in the ‘views’ or the ‘controller’?

my current code snippets::

app/controllers::  
def reply  
@messages = Messages.new  
if @messages.parent\_id = Messages.new  
flash[:notice] = ‘Your Reply was Posted.’  
render :action =\> ‘new’  
else  
@messages = Messages.new(params[:messages])  
end  
end

app/views::

reply.rhtml  
\<%= start\_form\_tag :action =\> ‘reply’ %\>  
\<%= render\_partial “form” %\>  
\<%= submit\_tag " REPLY " %\>  
\<%= end\_form\_tag %\>

\_form.rhtml

\<%= text\_field ‘messages’, ‘username’ %\>  
\<%= text\_field ‘messages’, ‘email’ %\>  
\<%= text\_field ‘messages’, ‘subject’ %\>  
\<%= text\_area ‘messages’, ‘message’ %\>  
\<%= hidden\_field ‘messages’, ‘id’ %\>  
\<%= hidden\_field ‘messages’, ‘root\_id’ %\>  
\<%= hidden\_field ‘messages’, ‘parent\_id’ %\>  
\<%= hidden\_field ‘messages’, ‘depth’ %\>  
\<%= hidden\_field ‘messages’, ‘Lft’ %\>  
\<%= hidden\_field ‘messages’, ‘Rgt’ %\>

show.rhtml  
\<% for column in Messages.content\_columns %\>  
\<% end %\>  
On \<%= @messages.created\_on %\>  
\<%= @messages.username %\>  
wrote: \<%= @messages.message %\>

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 3, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/acts-as-threaded-help/54912/6 "2006-05-03T19:52:04Z")

</div>

I am finding the answers in your tutorial

[http://www.railtie.net/plugins/acts\_as\_threaded/threaded.swf](http://www.railtie.net/plugins/acts_as_threaded/threaded.swf)

thanks,  
Monica
