Non-ruby-conform association question

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

  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-