# Challenge

**URL:** <https://www.ruby-forum.com/t/challenge/95592>\
**Category:** Rails\
**Created:** [November 14, 2005, 3:31pm UTC](https://www.ruby-forum.com/t/challenge/95592 "2005-11-14T15:31:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![allenbobo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@allenbobo](https://www.ruby-forum.com/u/allenbobo)\
**Post date:** [November 14, 2005, 3:31pm UTC](https://www.ruby-forum.com/t/challenge/95592/1 "2005-11-14T15:31:59Z")

</div>

i love ROR. but i met a problem :  
i have a table:  
users  
id int not null,  
name varchar(100)…

and i want create a friends association, seems many-to-many  
self-referential

i create a friends table:  
user\_id int not null,  
friend\_id int not null,  
constraint fk\_fu\_user foreign key (user\_id) references users(id),  
constraint fk\_fu\_friend foreign key (friend\_id) references users(id)  
…

the user Model contains  
has\_many :friends

the friend Model contains  
belongs\_to :user

i hope it’s ok ,but when i test it allways:

“Couldn’t find Firend without a ID”

so, guys, if you know what’s goning on, please give a hand.

all regards

---

<div class="post-metadata">

**Author:** ![allenbobo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@allenbobo](https://www.ruby-forum.com/u/allenbobo)\
**Post date:** [November 14, 2005, 3:38pm UTC](https://www.ruby-forum.com/t/challenge/95592/2 "2005-11-14T15:38:00Z")

</div>

There is a good article on this on the wiki

[http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship](http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship)

Maybe this will help

Cheers

---

<div class="post-metadata">

**Author:** ![allenbobo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@allenbobo](https://www.ruby-forum.com/u/allenbobo)\
**Post date:** [November 14, 2005, 3:50pm UTC](https://www.ruby-forum.com/t/challenge/95592/3 "2005-11-14T15:50:05Z")

</div>

Hello,

```
Don't know if this will help or not, but In the Active Record docs 

```

there  
is an ‘act as tree’ which works as a one to many on itself. It has allot  
of  
info about how this is done in the modal, you could most likely get some  
ideas from that as well.

Chanan Braunstein  
Knovel Corp.  
Web D. Manager  
607-773-1840 x672  
[http://www.knovel.com](http://www.knovel.com)

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Liquid  
Sent: Monday, November 14, 2005 9:36 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Rails] challenge

There is a good article on this on the wiki

[http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToMan](http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToMan)  
yRelationship  
\<[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToMa)  
nyRelationship\>

Maybe this will help

Cheers

On 11/15/05, allenbobo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

i love ROR. but i met a problem :  
i have a table:  
users  
id int not null,  
name varchar(100)…

and i want create a friends association, seems many-to-many  
self-referential

i create a friends table:  
user\_id int not null,  
friend\_id int not null,  
constraint fk\_fu\_user foreign key (user\_id) references users(id),  
constraint fk\_fu\_friend foreign key (friend\_id) references users(id)  
…

the user Model contains  
has\_many :friends

the friend Model contains  
belongs\_to :user

i hope it’s ok ,but when i test it allways:

“Couldn’t find Firend without a ID”

so, guys, if you know what’s goning on, please give a hand.

all regards

---

<div class="post-metadata">

**Author:** ![allenbobo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@allenbobo](https://www.ruby-forum.com/u/allenbobo)\
**Post date:** [November 14, 2005, 3:53pm UTC](https://www.ruby-forum.com/t/challenge/95592/4 "2005-11-14T15:53:05Z")

</div>

“Couldn’t find Firend without a ID”

I hope this is a typo and not copied and pasted from the error! In which  
case you would have to start looking at where you are trying to  
reference a “Firend” -note the typo

regards,

Franc Paul

---

<div class="post-metadata">

**Author:** ![allenbobo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@allenbobo](https://www.ruby-forum.com/u/allenbobo)\
**Post date:** [November 14, 2005, 4:42pm UTC](https://www.ruby-forum.com/t/challenge/95592/5 "2005-11-14T16:42:09Z")

</div>

thanks all the guys reply my “challenge”.  
and “liquid” give us a good link  
it’s solve this kind of problem.  
here:  
[http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship](http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship)

cheers

---

<div class="post-metadata">

**Author:** ![allenbobo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@allenbobo](https://www.ruby-forum.com/u/allenbobo)\
**Post date:** [November 22, 2005, 12:03am UTC](https://www.ruby-forum.com/t/challenge/95592/6 "2005-11-22T00:03:56Z")

</div>

I am having a similar problem, I have a category model which is defined  
to  
“acts\_as\_tree”. The table structure looks like this:

create table categories (  
id INT not null auto\_increment,  
name VARCHAR(200) not null,  
parent\_id int,  
description TEXT ,  
created\_on DATETIME null,  
updated\_on DATETIME null,  
constraint fk\_categories\_to\_categories foreign key (parent\_id)  
references  
categories(id),  
primary key (id)  
);

as you can see the foreign key is a reference to another row in the  
table.  
For the root category I just have (NULL) specified for the parent\_id.  
However all of my unit tests are blowing up because it cannot find the  
parent category for the root category (because it does not exist).

## ERROR MESSAGE:

1. Error:  
test\_create(CategoryTest):  
ActiveRecord::StatementInvalid: #23000Cannot delete or update a parent  
row:  
a foreign key constraint fails (`itsthes_test/categories`, CONSTRAINT  
`fk_ categories_to_categories` FOREIGN KEY (`parent_id`) REFERENCES  
`categories`  
(`id`)): DELETE FROM categories

* * *

Does anyone have an idea how to solve this problem?

Thanks!  
Mark

---

<div class="post-metadata">

**Author:** ![allenbobo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@allenbobo](https://www.ruby-forum.com/u/allenbobo)\
**Post date:** [November 22, 2005, 1:20am UTC](https://www.ruby-forum.com/t/challenge/95592/7 "2005-11-22T01:20:23Z")

</div>

Tada!  
I answered my own question. I am posting my solution in hopes that he  
helps  
other developers. The problem I was facing with the unit tests was that  
the  
foreign keys needed to be added and deleted from the fixtures in a  
particular order. The default YAML format was not helpful in this  
regard.  
Fortunately there is such a thing as the ordered mapping language  
independent type : [Ordered Mapping Language-Independent Type for YAML™&nbsp;Version&nbsp;1.1](http://yaml.org/type/omap.html)

Used in my category example it would look something like this:

— !omap

- parent:  
id: 1  
parent\_id: NULL  
title: Parent
- child:  
id: 2  
parent\_id: 1  
title: Child

Later,  
Mark
