Skip to content

Instantly share code, notes, and snippets.

@gamov
Created June 23, 2011 04:05
Show Gist options
  • Save gamov/1041878 to your computer and use it in GitHub Desktop.
Save gamov/1041878 to your computer and use it in GitHub Desktop.
Getting a different model from an association
class Shipment
has_many :shipping_items, :inverse_of => :shipment, :dependent => :destroy do
def linked_po_ids
joins(:purchased_item => :purchase_order).select('DISTINCT purchase_orders.id AS po_id').map{|si| si.po_id}
def linked_pos
? #how to return the purchase orders (objects) ?
end
end
def linked_pos
PurchaseOrder.joins(:purchased_items => :shipping_items).where(:shipping_items => {:shipment_id => self.id}).select('DISTINCT purchase_orders.*')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment