Created
July 5, 2016 20:45
-
-
Save mcelaney/d466adfad420a2dfe1fcde14bccecae9 to your computer and use it in GitHub Desktop.
This file contains 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
defmodule ProductVehicle do | |
def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do | |
query | |
|> join(:inner, | |
[products_vehicles], | |
product in assoc(products_vehicles, :product)) | |
|> join(:inner, | |
[products_vehicles], | |
vehicle in assoc(products_vehicles, :vehicle)) | |
|> join(:inner, | |
[_, _, vehicle], | |
vehicle in assoc(vehicle, :vehicle_model)) | |
end | |
def for_year(query \\ ProductVehicle, year) do | |
where(query, [_pa, _p, v], v.year == ^year) | |
end | |
end | |
# Which would be used like this: | |
ProductVehicle | |
|> vehicles_that_fit_product(42) | |
|> for_year("1961") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment