Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class PurchaseOrder
include DataMapper::Resource
storage_names[:default] = ...
property :id, Serial, :field => 'purchaseorderindex'
property :closed_date, Date, :field => 'closeddate'
has n, :items, :model => 'PurchaseOrderLineItem', :child_key => [:po_id, Integer], :deleted => false
class PurchaseOrderLineItem
include DataMapper::Resource
before :destroy, :check_receiving
storage_names[:default] = 'poitem'
property :id, Serial, :field => 'poitemindex'
property :cb3_id, Integer, :field => 'codebook3index'
property :po_id, Integer, :field => 'purchaseorderindex'
property :deleted, Boolean, :default => false, :field => 'deleteflag'
belongs_to :po, :model => 'PurchaseOrder', :child_key => [:po_id, Integer]
I want to get the lasted *purchased* PurchaseOrderLineItem which has the cb3_id = xxxx.
1. purchased - the closed purchase order
2. latest - has to be ordered by purchaseOrder closed_date.desc
3. I want the line item(not the order) which is specified by cb3_id
Here is the query(which is simplified):
PurchaseOrder.first(:completed => 'yes', :order => [:closed_date.desc], 'items.cb3_id' => 17633333).items.first(:cb3_id => 1763333)
I have to do the second cb3_id against the items, because that order might have several line items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.