Challenge

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

There is a good article on this on the wiki

http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship

Maybe this will help

Cheers

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


From: [email protected]
[mailto:[email protected]] On Behalf Of Liquid
Sent: Monday, November 14, 2005 9:36 AM
To: [email protected]
Subject: Re: [Rails] challenge

There is a good article on this on the wiki

http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToMan
yRelationship
<Peak Obsession
nyRelationship>

Maybe this will help

Cheers

On 11/15/05, allenbobo [email protected] 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

“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

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

cheers

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

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™ Version 1.1

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