Wednesday, November 23, 2011

[Rails] Awesome nested set - how to put there items?

hello guys,


I have following structure of the table, where I store items of tree:

| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | | NULL | |
| parent_id | int(11) | YES | | NULL | |
| lft | int(11) | YES | | NULL | |
| rgt | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+

In the form where I create a new items to the tree structure I have in a
hidden input always the parent of item, that I just create.

In a controller I do this:

def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
save_tree = TreeStruct.create!(:user_id => @user.id)
save_tree.move_to_child_of(params[:parent])

format.html { redirect_to(root_url, :notice => 'OK.') }
format.xml { render :xml => @user, :status => :created,
:location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status =>
:unprocessable_entity }
end
end
end

But always when I try to create and item to this tree, I will get the
error:

Couldn't find TreeStruct with id=28

The number **28** is the ID od parent item. The currently created item
saved to database, bud the column **parent_id** has the value **NULL**.

I am a bit confusing of it - what I am doing wrong? Why I am getting
this error? And how I should create a root of the tree and then add
other items?

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate