Legacy system belongs_to problem

Hi all,

I am trying to patch a small rails app into a large system we have at
our company. The legacy has a large database that naturally does not
match the Rails conventions, but this has not been a serious problem
thanks to rails flexibility.

The problem I just ran into is that my “LineItems” table links back to
the “Products” table using a modified foreign key. For example, the
ProductID in the LineItem table as a “LI-” prefixed to the productID. So
they link together as follows:

LineItem (ProductID) Product (ID)
IN-123456 -> 123456
IN-245435 -> 245435
etc…

The standard belongs_to declaration will not work for this. Can anyone
tell me how to get around this with belongs_to or do I have to do the
linking manually?

Thanks

Peer

Peer A. wrote:

Hi all,

I am trying to patch a small rails app into a large system we have at
our company. The legacy has a large database that naturally does not
match the Rails conventions, but this has not been a serious problem
thanks to rails flexibility.

The problem I just ran into is that my “LineItems” table links back to
the “Products” table using a modified foreign key. For example, the
ProductID in the LineItem table as a “LI-” prefixed to the productID. So
they link together as follows:

LineItem (ProductID) Product (ID)
IN-123456 -> 123456
IN-245435 -> 245435
etc…

The standard belongs_to declaration will not work for this. Can anyone
tell me how to get around this with belongs_to or do I have to do the
linking manually?

Thanks

Peer

I have a silly question.
You stated that the LineItem entries have a key with LI- prefixed to
them, but you show entries with IN- prefixed.

Is this wrong? or am I mis-understanding?

Perhaps you could adapt the “using models as join tables” example in
AWDWR chapter 18.3?

Sean