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:
-
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… -
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,…)