# Model question

**URL:** <https://www.ruby-forum.com/t/model-question/124880>\
**Category:** Rails\
**Created:** [January 3, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/model-question/124880 "2008-01-03T14:10:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 3, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/model-question/124880/1 "2008-01-03T14:10:26Z")

</div>

Situation: we’re creating an application for orders and order  
tracking.  
base class : Qoi (quotations, orders, invoices).  
Quotations \< Qoi, Orders \< Qoi , Invoices \< Qoi

Each Qoi has\_many :line\_items (each line item has unit price,  
amount,…)  
Each line\_item  
–\> has\_many :rfq\_of (Request\_for-Quote + Order\_Form)  
for some items we must ask for price to our suppliers so we must  
create one or more requests for quote  
and  
–\> has\_many :log\_items  
These are used for logging deadlines, phone calls…

When a Quotation becomes an Order we copy the line\_items. This way we  
can keep track when things change during the lifetime of an order (eg  
the amount changes, so we can still see that in the Quotation we  
offered 3 MacBooks, but the client ordered only 2.

Now the problems start to bubble up… When copying a line\_item we  
loose the relation to all rfq\_of and log\_items…  
We tried to solve this using an extra id in a line\_item : base\_id.  
When creating a new line\_item we set base\_id = id, when copying a  
line\_item, base\_id is just copied. This way we can track back to the  
original line\_item and find all rfq\_of’s and log\_items…

We would need a relation from base\_id (in line\_items) to  
line\_item\_base\_id (in rfq\_of), but afaik rails doesn’t support  
relations that start from a differente field than id…

We could do this in a few ways:

1. in line items we could write : belongs\_to :base\_record, :class =\>  
“LineItem”, :foreign\_key =\> :base\_id  
this way we can retrieve the ‘base’ record and it’s rfq’s via:  
@line\_item.base\_record.rfqs  
—\> is will give us trouble if for some reason the original line\_item  
in the Quotation is deleted…

2. we could not use relations but do a find  
RfqOf.find(:all, :conditions {:line\_item\_base\_id =\> base\_id})  
I can’t give a logical explanation but we don’t like this solution, it  
looks overly complex. And we will need to write a lot of 'search  
methods for everything

Both solutions also have the problem that we can’t use :dependent  
=\> :destroy, in the first case rfq\_of’s would be destroyed if the base  
line\_item was destroyed, in the second case it can’t be automated.  
Rfq\_of (and log\_items) should be destroyed if all line\_items of the  
same family (same base\_id) are destroyed.

Is there better way, either by not using the base\_id or a completely  
different structure… (extra table, wrapper,…)
