Expecting field value and getting record

The following short console session illustrates the problem:

Loading development environment (Rails 2.3.2)

topic=BlogTopic.find(4)

=> #<BlogTopic id: 4, parent: 2, topic: "Topic Four", created_at: "2009-07-13 21:31:22", updated_at: "2009-09-14 00:37:29">

topic.inspect

=> "#<BlogTopic id: 4, parent: 2, topic: \"Topic Four\", created_at: \"2009-07-13 21:31:22\", updated_at: \"2009-09-14 00:37:29\">"

topic.parent

=> #<BlogTopic id: 2, parent: 0, topic: "Topic Two", created_at: "2009-07-13 21:29:02", updated_at: "2009-07-29 21:11:46">

The variable 'topic' is supposed to get an instance of the BlogTopic object corresponding to the DB record with and id of 4. The console initialization seems to indicate that that happens and the 'topic.inspect' in the second line confirms it.

In the third and final line, I would expect topic.parent to return '2' as the record value for the parent field. Instead, I actually get the full entire parent record. I should say that I am using acts_as_tree and 'parent' is my foreign key. I'm thinking that might have something to do with it.

Anyway, I'm trying to figure out why this is happening; and, how I can get at the value of the parent field in topic. Anyone have any suggestions?

Thanks for any input.

         ... doug

The following short console session illustrates the problem:

Loading development environment (Rails 2.3.2)

topic=BlogTopic.find(4)

=> #<BlogTopic id: 4, parent: 2, topic: "Topic Four", created_at: "2009-07-13 21:31:22", updated_at: "2009-09-14 00:37:29">

topic.inspect

=> "#<BlogTopic id: 4, parent: 2, topic: \"Topic Four\", created_at: \"2009-07-13 21:31:22\", updated_at: \"2009-09-14 00:37:29\">"

topic.parent

=> #<BlogTopic id: 2, parent: 0, topic: "Topic Two", created_at: "2009-07-13 21:29:02", updated_at: "2009-07-29 21:11:46">

The variable 'topic' is supposed to get an instance of the BlogTopic object corresponding to the DB record with and id of 4. The console initialization seems to indicate that that happens and the 'topic.inspect' in the second line confirms it.

In the third and final line, I would expect topic.parent to return '2' as the record value for the parent field. Instead, I actually get the full entire parent record. I should say that I am using acts_as_tree and 'parent' is my foreign key. I'm thinking that might have something to do with it.

If you were to use parent_id as the field name, which would be more conventional and arguably more intuitive, then topic.parent_id would give you the id and topic.parent would give you the object.

As an aside I have found that when I find myself manipulating the ids then often it means I am not doing something the best way, not always but often. Very often rails can do a lot of the magic for me, removing the need to use the ids.

Colin

If you were to use parent_id as the field name, which would be more conventional and arguably more intuitive, then topic.parent_id would give you the id and topic.parent would give you the object.

I agree with you that the use of 'parent_id' for the foreign field would have been a much better choice for a variety of reasons including those that you cite. The thing is that the use of acts_as_tree came along late in the game long after the database had been setup and long after a lot of code had already been written based on that field being named 'parent'. So, I just thought that since acts_as_tree allows me to designate a foreign field with a name other than the default, the easy thing to do would be to just make the foreign field, 'parent' so that it would mesh with the existing database. I had no idea that it was going to open Pandora's Box.

Having said all of that, the thing that really grabed me about your comment is this:

then topic.parent_id would give you the id and topic.parent would give you the object.

Why would topic.parent give me the object? Is that an acts_as_tree thing? I thought all acts_as_tree did was add the 3 methods, 'ancestors', 'root', and 'siblings'. As far as I know, 'parent' is not a reserved word in Ruby. However, based on what you say, it would seem that the choice of 'parent' as the field name may be an *EXTREMELY* poor choice (even worse than I had thought). Perhaps I would have been better off if it had been named 'dad' or 'mom' or something. Anyway, I won't know the answer to that until I understand why topic.parent would return the object. May I impose on you a bit further to tell me why that is. Based on your answer, I may be FORCED to change the name of that field.

Thanks a lot.

          ... doug

> then topic.parent_id would > give you the id and topic.parent would give you the object.

Why would topic.parent give me the object? Is that an acts_as_tree thing? I thought all acts_as_tree did was add the 3 methods, 'ancestors', 'root', and 'siblings'. As far as I know, 'parent' is

acts as tree adds methods parent, children as well (or rather associations but net effect is the same)

Fred

> then topic.parent_id would > give you the id and topic.parent would give you the object.

Why would topic.parent give me the object? Is that an acts_as_tree thing? I thought all acts_as_tree did was add the 3 methods, 'ancestors', 'root', and 'siblings'. As far as I know, 'parent' is

acts as tree adds methods parent, children as well (or rather associations but net effect is the same)

See http://api.rubyonrails.org/classes/ActiveRecord/Acts/Tree/ClassMethods.html

Colin

> acts as tree adds methods parent, children as well (or rather > associations but net effect is the same)

WOW!! Well then, that's my problem. I can't have a foreign-key column named 'parent'. It's a conflict which is going to produce exactly the kind of difficulties that I'm seeing. I'm going to have to re-name that column (ugh!). Having a foreign key column named 'parent' is a *REALLY* bad choice.

Thanks a batch for the help!!

         ... doug