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?

On Aug 21, 12:16 am, nick ger [email protected]
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

Also to clarify why I need this. I could run a second statement in
controller where I find the OldRma, but it’s slower then running an
include, and OldRma - has other associations that need to be used, so an
include would give me an option to have everything in one variable -
which makes it easier to iterate through in a view, than trying to pull
info from multiple variable, or doing w.oldrma every time I need
something. Could easily get out of hand when you need to pull 100’s of
records and display them. ( pagination is not an option. don’t ask me
why, I just can’t explain to client then nobody wants to scroll down
100’s of rows but…)

Frederick C. wrote:

On Aug 21, 12:16�am, nick ger [email protected]
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.

On Aug 21, 12:27 am, nick ger [email protected]
wrote:

both has_many and belongs_to take a :primary_key option (which

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

Also the reason your previous suggestion wouldn’t work, cause when you
specify foreign_key, rails assumes, that it’s a primary key in the table
you are referencing. Which it is not. ref_number field is just a varchar
field , that while unique is not a primary key in the OldRma model, and
can’t be change to be one.

Fred,
Perhaps a diagram would explain it better:

http://www.gorbikoff.com/stuff/rails.png

the relationship I’m trying to recreate is in red.

You can see that I can’t change the primary keys as they are already
used in other models. So I need to create a custom association here.

Nick

Also I don’t want to add oldrma_id and work_order_id to tables - cause
then I have an extra variable to keep track off.

And has_many_and_belong_to wouldn’t work, cause again I ref_number is
not a primary key on either table and it’s their for history reasons.

On Aug 21, 1:05 am, nick ger [email protected] wrote:

Also the reason your previous suggestion wouldn’t work, cause when you
specify foreign_key, rails assumes, that it’s a primary key in the table
you are referencing. Which it is not. ref_number field is just a varchar
field , that while unique is not a primary key in the OldRma model, and
can’t be change to be one.

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