# Active Record Question

**URL:** <https://www.ruby-forum.com/t/active-record-question/120970>\
**Category:** Rails\
**Created:** [November 22, 2007, 9:48am UTC](https://www.ruby-forum.com/t/active-record-question/120970 "2007-11-22T09:48:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 22, 2007, 9:48am UTC](https://www.ruby-forum.com/t/active-record-question/120970/1 "2007-11-22T09:48:08Z")

</div>

hey!

I have the following table structure in my DB

| users |
| --- |
| (pk)id |
| username |
| pass |

* * *

```
 (1:n)

```

| users\_objects | | objectattributes |

* * *

| (fk,pk)user\_id | (1:n) | (pk)attribute |  
|(fk,pk)object\_id| | (pk,fk) users\_objects\_user\_id |  
| … | |(pk,fk) users\_objects\_object\_id |  
------------------ | value |  
----------------------------------  
(1:n)

| objects |
| --- |
| (pk)id |
| … |
| … |

* * *

So basically i have a many to many relation (with attributes) between  
users and objects and a 1 to many relation between users\_objects and  
objectattributes.

I have introduced models for user, objects and users\_objects. The many  
to many realtion is modeled with the :through associaction.

How does the relation between users\_objects and objectattributes in  
ActiveRecord look like?

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 22, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/active-record-question/120970/2 "2007-11-22T17:30:27Z")

</div>

Christian K. wrote:

> I have the following table structure in my DB
> 
> | users |
> | --- |
> | (pk)id |
> | username |
> | pass |
> 
> * * *
> 
> ```
> (1:n)
> 
> ```
> 
> | users\_objects | | objectattributes |
> 
> * * *
> 
> | (fk,pk)user\_id | (1:n) | (pk)attribute |  
> |(fk,pk)object\_id| | (pk,fk) users\_objects\_user\_id |  
> | … | |(pk,fk) users\_objects\_object\_id |  
> ------------------ | value |  
> ----------------------------------  
> (1:n)
> 
> | objects |
> | --- |
> | (pk)id |
> | … |
> | … |
> 
> * * *
> 
> So basically i have a many to many relation (with attributes) between  
> users and objects and a 1 to many relation between users\_objects and  
> objectattributes.
> 
> I have introduced models for user, objects and users\_objects. The many  
> to many realtion is modeled with the :through associaction.
> 
> How does the relation between users\_objects and objectattributes in  
> ActiveRecord look like?

ActiveRecord doesn’t understand composite primary keys. You’ll have to  
create an (pd)id field in users\_objects. (You’ll need that anyway if  
you’re using has\_many :through.) Then objectattributes can use that as  
the fk for users\_objects\_id.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 23, 2007, 8:41am UTC](https://www.ruby-forum.com/t/active-record-question/120970/3 "2007-11-23T08:41:28Z")

</div>

Josh S. wrote:

> ActiveRecord doesn’t understand composite primary keys. You’ll have to  
> create an (pd)id field in users\_objects. (You’ll need that anyway if  
> you’re using has\_many :through.) Then objectattributes can use that as  
> the fk for users\_objects\_id.

I have installed the composite\_keys extension for active record. Now it  
seems to work with composite keys as expected.
