# Expecting field value and getting record

**URL:** <https://www.ruby-forum.com/t/expecting-field-value-and-getting-record/175446>\
**Category:** Rails\
**Created:** [September 14, 2009, 3:00am UTC](https://www.ruby-forum.com/t/expecting-field-value-and-getting-record/175446 "2009-09-14T03:00:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [September 14, 2009, 3:00am UTC](https://www.ruby-forum.com/t/expecting-field-value-and-getting-record/175446/1 "2009-09-14T03:00:51Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [September 14, 2009, 9:44am UTC](https://www.ruby-forum.com/t/expecting-field-value-and-getting-record/175446/2 "2009-09-14T09:44:44Z")

</div>

2009/9/14 doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > topic.parent  
> > > 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

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [September 14, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/expecting-field-value-and-getting-record/175446/3 "2009-09-14T18:16:58Z")

</div>

> 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
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [September 14, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/expecting-field-value-and-getting-record/175446/4 "2009-09-14T19:44:13Z")

</div>

On Sep 14, 5:15 pm, djolley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [September 14, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/expecting-field-value-and-getting-record/175446/5 "2009-09-14T20:04:43Z")

</div>

> > 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
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [September 14, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/expecting-field-value-and-getting-record/175446/6 "2009-09-14T19:47:09Z")

</div>

2009/9/14 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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](http://api.rubyonrails.org/classes/ActiveRecord/Acts/Tree/ClassMethods.html)

Colin
