Hi,
I''m using the latest gem version of rails under MacOS Tiger.
I was trying to use acts_as_tree as described in the rails book. So
I have something like:
class Category < ActiveRecord::Base
acts_as_tree :order => "title"
end
create table categories (
id int not null
auto_increment,
title varchar(100) not null,
parent_id int,
constraint fk_category foreign key (parent_id)
references categories(id),
primary key (id)
);
When I try to use category.children I get an invalid member error.
When I changed this to:
class Category < ActiveRecord::Base
belongs_to :parent,
:class_name => "Category"
has_many :children,
:class_name => "Category",
:foreign_key => "parent_id",
:order => "title",
:dependent => true
end
as also described in the rails book I get the expected behaviour --
namely children works as expected :)
However, this also has an issue... category.parent is always nil,
whether or not the category has a parent. That is
Category.find(child.parent_id.to_i) works, but child.parent itself is
nil.
Any ideas?
Matthew
Matthew Newhook wrote:> Category.find(child.parent_id.to_i) works, but child.parent itself is nil.How are you instantiating the variable "child"? -- We develop, watch us RoR, in numbers too big to ignore.
It goes something like this:
def show
@category = Category.find(params[:id])
@categories = @category.children
end
and then in the show.rhtml I''m preparing a stack to print the
hierarchy.
stack = []
root = @category
while root != nil
stack.push(root)
if root.parent_id == nil
root = nil
else
root = Category.find(root.parent_id.to_i)
end
# root = root.parent doesn''t work. What''s up with that?
end
Matthew
On 23-Sep-05, at 1:09 PM, Mark Reginald James wrote:
> Matthew Newhook wrote:
>
>
>> Category.find(child.parent_id.to_i) works, but child.parent itself
>> is nil.
>>
>
> How are you instantiating the variable "child"?
>
> --
> We develop, watch us RoR, in numbers too big to ignore.
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Matthew Newhook wrote:> It goes something like this: > > def show > @category = Category.find(params[:id]) > @categories = @category.children > end > > and then in the show.rhtml I''m preparing a stack to print the hierarchy. > > stack = [] > root = @category > while root != nil > stack.push(root) > if root.parent_id == nil > root = nil > else > root = Category.find(root.parent_id.to_i) > end > # root = root.parent doesn''t work. What''s up with that? > endThe parent_id of your root categories is probably 0 rather than nil (hence your use of to_i). Try replacing the line: if root.parent_id == nil with either if root.parent.nil? or if root.parent_id == 0 -- We develop, watch us RoR, in numbers too big to ignore.
I''m not sure what you are saying. I was testing with two records in the database much like this: id title parent_id 1 Root <null> 2 Foo 1 When I tried calling root.parent for Foo (ie: id == 2) I was returned nil. AFAIK, that is not correct. It should have returned the Root category (namely the category with id == 1). Matthew On 23-Sep-05, at 2:51 PM, Mark Reginald James wrote:> Matthew Newhook wrote: > >> It goes something like this: >> def show >> @category = Category.find(params[:id]) >> @categories = @category.children >> end >> and then in the show.rhtml I''m preparing a stack to print the >> hierarchy. >> stack = [] >> root = @category >> while root != nil >> stack.push(root) >> if root.parent_id == nil >> root = nil >> else >> root = Category.find(root.parent_id.to_i) >> end >> # root = root.parent doesn''t work. What''s up with that? >> end >> > > The parent_id of your root categories is probably 0 rather than > nil (hence your use of to_i). > > Try replacing the line: > if root.parent_id == nil > with either > if root.parent.nil? > or > if root.parent_id == 0 > > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Matthew Newhook wrote:> I''m not sure what you are saying. I was testing with two records in the > database much like this: > > id title parent_id > 1 Root <null> > 2 Foo 1 > > When I tried calling root.parent for Foo (ie: id == 2) I was returned > nil. AFAIK, that is not correct. It should have returned the Root > category (namely the category with id == 1).Hmmm. The parent method works fine for me in Rails 0.13.1. -- We develop, watch us RoR, in numbers too big to ignore.
#root is a new acts_as_tree instance method, so perhaps there is a naming conflict with your use of ''root''? On a related note, you should now be able to do stack = @category.ancestors to replace the show.rhtml code you mentioned. -Scott Reilly (coffee2code) On 9/23/05, Matthew Newhook <matthew.newhook-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''m not sure what you are saying. I was testing with two records in > the database much like this: > > id title parent_id > 1 Root <null> > 2 Foo 1 > > When I tried calling root.parent for Foo (ie: id == 2) I was > returned nil. AFAIK, that is not correct. It should have returned the > Root category (namely the category with id == 1). > > Matthew > > On 23-Sep-05, at 2:51 PM, Mark Reginald James wrote: > > > Matthew Newhook wrote: > > > >> It goes something like this: > >> def show > >> @category = Category.find(params[:id]) > >> @categories = @category.children > >> end > >> and then in the show.rhtml I''m preparing a stack to print the > >> hierarchy. > >> stack = [] > >> root = @category > >> while root != nil > >> stack.push(root) > >> if root.parent_id == nil > >> root = nil > >> else > >> root = Category.find(root.parent_id.to_i) > >> end > >> # root = root.parent doesn''t work. What''s up with that? > >> end > >> > > > > The parent_id of your root categories is probably 0 rather than > > nil (hence your use of to_i). > > > > Try replacing the line: > > if root.parent_id == nil > > with either > > if root.parent.nil? > > or > > if root.parent_id == 0 > > > > > > -- > > We develop, watch us RoR, in numbers too big to ignore. > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails