custom associations

Hi, I was wondering if somebody has any pointer for me.
Here is what I have and trying to do.

Let's say I have 2 models

WorkOrder ( id, ref_number, desc) ( and a bunch of other fields)

OldRma ( id, ref_number, customer_data)

Work order is a big table that has all the info about the work order,
Old rma contains older data, that is still imported from customer that
needs to be referenced. Both models hav id as a primary field that can't
be changed ( so I can't make ref_number a primary key in either table)

ref_number field is unique for OldRma, and there might be multiple
WorkOrders with the same ref_number.

I need to be able to reference both tables and run find statements with
includes.

At first I wanted to change primary key on OldRma, but that is not an
option as that field id is already used in other models.

I was able to add to WorkOrder

class WorkOrder < ActiveRecord::Base

  def oldrma
     OldRma.find_by_ref_number(seld.ref_number)
  end

end

So I can do:

w = WorkOrder.find_by_ref_number(5)
w.oldrma

Which kind of lets me have shortcuts for finding data.
But what I need to do is

w = WorkOrder.find(:all, :conditions => "work_orders.ref_number = 1234",
:include = > :oldrma )

This obviously breaks cause there is no direct association between two
models.
belongs_to with a foreign_key - won't work cause I can't change the
primary keys on models. Rails doesn't support secondary keys. So can
somebody suggest a way to do it?

Any ideas?

both has_many and belongs_to take a :primary_key option (which
defaults to id). Is this what you are looking for?

Fred

Frederick Cheung wrote:

This obviously breaks cause there is no direct association between two
models.
�belongs_to with a foreign_key - won't work cause I can't change the
primary keys on models. Rails doesn't support secondary keys. So can
somebody suggest a way to do it?

both has_many and belongs_to take a :primary_key option (which
defaults to id). Is this what you are looking for?

Fred

Thanks for a reply Fred. But is the opposite of what I'm looking for.
:slight_smile:

I need to be able to leave primary_key (:id) as they are on the table
right now, but also create another association between tables using
ref_number field.

So Let's say I want legacy app to be able to access table using :id
field. And these two tables communicate to each other using association
on :ref_number field. I know it's not the best of designs, but I have to
deal with what I've got.

Frederick Cheung wrote:

>> This obviously breaks cause there is no direct association between two
>> models.
>> belongs_to with a foreign_key - won't work cause I can't change the
>> primary keys on models. Rails doesn't support secondary keys. So can
>> somebody suggest a way to do it?

> both has_many and belongs_to take a :primary_key option (which
> defaults to id). Is this what you are looking for?

> Fred

Thanks for a reply Fred. But is the opposite of what I'm looking for.
:slight_smile:

I need to be able to leave primary_key (:id) as they are on the table
right now, but also create another association between tables using
ref_number field.

Isn't that precisely what the primary_key option does ?

if you do belongs_to :other_class, :foreign_key => 'ref_number'

then activerecord will look for a row in that table whose primary key
is equal to the object's ref number. If you say :primary_key =>
'other_column' it will look for rows in that table where other_column
has that value (and similarly for has_many) - these options don't
change the table structure, just what the association uses to refer to
that table.

Fred

THat's the whole point of the primary key option. It says, for this
association only, link the foreign key up to this column instead of
linking it up to the table's primary key. It doesn't change the
table's primary key and it doesn't affect any other relationships. It
doesn't matter if the other column isn't the table's primary key
(after all if it were you wouldn't need this option at all).
Seriously, try it.

Fred