Created
February 13, 2019 15:32
-
-
Save artemave/4c9dd5b1f0c6255e95b3cf0d8c560c88 to your computer and use it in GitHub Desktop.
Filtered "top of the group" has_many association with a :through conterpart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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