Acts_as_tree, acts_as_list and is invincible (help!)

So I have the following model

class Topic < ActiveRecord::Base
belongs_to :topic
has_many :topics, :order => :position
acts_as_tree :order => :position
acts_as_list :scope => :topic_id
end

that I use for a site’s navigation. Everything (list, create, edit, even
the drag&drop sort) works fine, except the destroy function, which the
vanilla version

def destroy
Topic.find(params[:id]).destroy
redirect_to :action => ‘list’
end

which gives me the following error

“Mysql::Error: Unknown column ‘topics.parent_id’ in ‘where clause’:
SELECT * FROM topics WHERE (topics.parent_id = 39) ORDER BY position”

Now, my table has the normal “id” field and a “topic_id” field that I am
using instead of “parent_id”, so I have have both - a list and a tree.
And, like I said, it works great and I was surprised at how simple the
whole thing was - until I tried to delete a topic and the whole thing
exploded.

I also already tried

belongs_to :topic, :foreign_key => ‘topic_id’, :class_name => ‘Topic’

but that producced the same error.

So. Uhm. Help? :o)

Thanks for your time guys!

Samo

Hi,

Shouldn’t you specifiy the options foreign_key in your acts_as_tree call
? As you do not use the default column name parent_id, I would try
something like that :

acts_as_tree :order => :position, :foreign_key => ‘topic_id’

Bye,
Thomas.

I’m a newbie when it comes to Rails, so I didn’t know that (actually I
tried the foreign_key at all the wrong places…). Anyhow, thanks a lot

  • that solved it!

Samo

Thomas wrote:

Hi,

Shouldn’t you specifiy the options foreign_key in your acts_as_tree call
? As you do not use the default column name parent_id, I would try
something like that :

acts_as_tree :order => :position, :foreign_key => ‘topic_id’

Bye,
Thomas.