# Relationing has\_many, belongs\_to

**URL:** <https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632>\
**Category:** Rails\
**Created:** [September 3, 2007, 6:48am UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632 "2007-09-03T06:48:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [September 3, 2007, 6:48am UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632/1 "2007-09-03T06:48:08Z")

</div>

Hello, i want to know if i am relationing fine, because i am having  
problems,i have 2 models, one for members and other to relate some  
members to this:

class Member \< ActiveRecord::Base  
has\_many :matrix, :foreign\_key=\>:parent  
end

class Matrix \< ActiveRecord::Base  
set\_table\_name :matrix  
set\_primary\_key :userid  
belongs\_to :member, :foreign\_key=\>:userid  
acts\_as\_tree :foreign\_key=\>:parent  
end

when i try to save a new member it says: undefined method `userid=’ for  
Matrix:Class and i think is because he wants to do find() first, but i  
dont want it, because i am saving a new.

def create  
@member = Member.new(params[:member])  
@member.matrix.new  
@member.matrix.userid = @member.userid  
@member.matrix.parent = params[:parent]  
…  
end

is a relationating problem??

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [September 3, 2007, 8:33am UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632/2 "2007-09-03T08:33:38Z")

</div>

userid cannot be both the primary key and a foreign key.

Rein

On Sep 2, 11:48 pm, Edgar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [September 3, 2007, 1:01pm UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632/3 "2007-09-03T13:01:35Z")

</div>

Well the problem is, that the table schema is thus, because in some case  
i would rather use the rails convention an use member\_id, in case of  
userid, but i have to work with the actual table.

but my problem i dont think that is with this.

by the way, thanks Rein

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [September 3, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632/4 "2007-09-03T15:25:32Z")

</div>

@member.matrix.new returns a new matrix object. you have to assign it  
to a variable:

def create  
@member = Member.new(params[:member])  
@new\_matrix = @member.matrix.new  
@new\_matrix.userid = @member.userid  
@new\_matrix.parent = params[:parent]  
…  
end

about foreign keys: in the has\_many, you define parent as the foreign  
key, in the belongs\_to, you define userid as foreign key. but it has  
to be the same on both sides, otherwise it’s no proper relation,  
legacy Database or not.  
You tell the Member model that is has many Matrix’ associated through  
the parent column in the matrix table, but you tell the Matric Model  
that it belongs to a Member associated by the userid column in the  
matrix table … it should say “parent” here to, because the table on  
the has\_many side doesn’t have a foreign key, only the belongs\_ to  
side

On 3 Sep., 13:01, Edgar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [September 3, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632/5 "2007-09-03T20:31:51Z")

</div>

i still got the problem when i try to save through has\_many

def create  
@member = Member.new(params[:member])  
@new\_matrix = Matrix.new  
@new\_matrix.parent = params[:parent]

```
respond_to do |format|
  if @member.save
     @new_matrix.userid = @member.id
     @new_matrix.save
   .....
  end
end

```

end

undefined method `userid=’ for #Matrix:0x48a4450

but there is a column called userid! :S

i am consufused now

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [September 3, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632/6 "2007-09-03T20:44:51Z")

</div>

You’ve defined userid as the primary key, which makes it accessable as  
id (ie @matrix.id) rather than userid. The problem here, though, is  
manifold:

Rails expects your database to be sane, which includes an id column  
that is a unique auto-incrementing integer.

Using it as a foreign key means that you’ll have to _set_ the primary  
key of the record, which is a huge no-no in both rails and database  
design and violates the auto-incrementing requirement.

Using is as a foreign key also implies that two records could share  
the same foreign key, and therefore the same primary key, which  
violates the unique requirement.

Rails can work with most legacy databases provided that they’re  
relatively sane. Using a primary key as a foreign key is most  
definitely insane and I doubt you’d make that table work properly with  
or without rails.

Rein

On Sep 3, 1:31 pm, Edgar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [September 3, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632/7 "2007-09-03T22:40:41Z")

</div>

Thanks a lot Rein, the problem is that it was a old system and now i am  
trying to move on rails.

now i got:

class Matrix \< ActiveRecord::Base  
set\_table\_name :matrix  
belongs\_to :member, :foreign\_key=\>:userid  
acts\_as\_tree :foreign\_key=\>:parent

attr\_writer :name  
attr\_writer :city  
attr\_writer :address  
attr\_writer :identification  
attr\_writer :phone  
attr\_writer :sponsor  
attr\_writer :email

end

i have put all these ‘att\_r\_writer’ because this:

def create  
@member = Member.new(params[:member])  
@new\_matrix = Matrix.new  
@new\_matrix.parent = params[:member] \<\<\<\<— here is my problem  
respond\_to do |format|  
if @member.save  
@new\_matrix.userid = @member.id  
@new\_matrix.save  
…  
end  
end

how can i access to the params[:parent]??, because tis param is inside  
the params[:member]

Request

Parameters: {“commit”=\>“Create”, “member”=\>{“name”=\>“Edgar /8”,  
“city”=\>“sdfsdf”, “identification”=\>“s44546”, “phone”=\>“N/A”,  
“sponsor”=\>“0”, “address”=\>“sd”, “email”=\>“removed\_email\_address@domain.invalid”, “parent”=\>“5”}}

i tried params[:member][:parent], params[:parent]

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [September 3, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/relationing-has-many-belongs-to/113632/8 "2007-09-03T19:53:00Z")

</div>

Thanks Thorsten,

I got first part, but about the correct relation, i didn’t understood  
right, could you explain me better?

Thanks
