# Non-ruby-conform association question

**URL:** <https://www.ruby-forum.com/t/non-ruby-conform-association-question/91186>\
**Category:** Rails\
**Created:** [April 5, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/non-ruby-conform-association-question/91186 "2007-04-05T13:21:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [April 5, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/non-ruby-conform-association-question/91186/1 "2007-04-05T13:21:33Z")

</div>

Hi Folks,

I have three tables:

history  
hist\_id =\> Primary Key  
job\_id  
status\_id  
status\_time  
result\_id

status  
status\_id =\> Primary Key  
status\_name

params  
job\_id  
param\_name  
param\_value

History contain something like this:

hist\_id job\_id result\_id status\_id  
status\_time  
121 cd2e85b0-c584-0129-0370-0016350d87a7 10600303 7  
2007-04-05 11:20:26.0  
120 cd2e85b0-c584-0129-0370-0016350d87a7 4 2007-04-05  
11:20:23.0  
119 cd2e85b0-c584-0129-0370-0016350d87a7 1 2007-04-05  
11:20:22.0

Status contents look like this:

status\_id status\_name  
7 FINISHED  
6 CANCELLED  
5 FAILED  
4 PROCESSING  
3 WAITING  
2 STARTED  
1 INITIALIZED

And Params contains lots entries like this:

job\_id name value  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_1 “value 1”  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_2 “value 2”  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_3 NULL  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_4 NULL  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_5 NULL  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_6 “value 3”  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_7 “value 4”  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_8 NULL  
cd2e85b0-c584-0129-0370-0016350d87a7 param\_9 NULL  
[…]

What I need to do for display/scaffoldingis is this:  
From History view I want to display the status\_name instead of the  
status\_id From History view I need to access one or more of the params  
assigned to the job\_id

My first approach went wrong:  
In history.rb I added

set\_primary\_key “hist\_id”  
has\_one :status, :foreign\_key =\> “status\_id”  
has\_many :params, :foreign\_key =\> “job\_id”

And ActiveRecord tries to execute this  
SELECT \* FROM PARAMS WHERE (PARAMS.job\_id = ‘121’)

or

SELECT \* FROM STATUS WHERE (STATUS.status\_id = ‘121’)

As you can see in the History table the value for primary key “hist\_id”  
is “121”. So it looks like ActiveRecord tries to compare apples and  
oranges.

For the status association I could help my self by replacing the has\_one  
to a belongs\_to relation. This sounds somehow wrong because a history  
has\_a/has\_one status for each entry. The params relationship only works,  
when I use set\_primary\_key “job\_id” in history.rb which leads to other  
bad problems because job\_id is not unique as you can see.

To me it looks just like ActiveRecord is too unflexible to handle custom  
mappings. I would expect ActiveRecord to use something like a local\_key  
setting to tell that the foreign\_key does not map to the local  
primary\_key.

Am I wrong with my approach? Maybe I have to handle my associations  
totally diferent.

Thanks for any suggestions!

Regards,

Christian

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [April 5, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/non-ruby-conform-association-question/91186/2 "2007-04-05T13:48:08Z")

</div>

1. It was correct to replace the has\_one with belongs\_to, as the  
histories table holds the foreign key, and not the status table.  
in a has\_one or has\_many association, AR looks up the primary key  
in the assiociated table, not in its own.

2. the second association is - if you think about it - also not a  
has\_many relationship, because here the histories tabel also contains  
the foreign key.  
So each item in the history table can only be assosiated to one entry  
of the params table, because each item in “histories” can only have  
von job\_id.  
or do i get something wrong?  
Anyways, as it appears to me, this is a belongs\_to association, like  
the first one. give it a try.

On 5 Apr., 13:21, Christian S. \<rails-mailing-l…@andreas-
