Skip to content

Instantly share code, notes, and snippets.

@artemave
Created February 13, 2019 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemave/4c9dd5b1f0c6255e95b3cf0d8c560c88 to your computer and use it in GitHub Desktop.
Save artemave/4c9dd5b1f0c6255e95b3cf0d8c560c88 to your computer and use it in GitHub Desktop.
Filtered "top of the group" has_many association with a :through conterpart
scope :latest_prices, lambda {
joins('
LEFT JOIN client_products filter
ON filter.product_id = "client_products"."product_id"
AND "client_products"."created_at" < filter.created_at')
.where('filter.id IS NULL')
.where('"client_products"."price_per_pack" > 0')
}
has_many :client_products, -> { merge Client.latest_prices }
has_many :products, -> { merge Client.latest_prices }, through: :client_products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment