Skip to content

Instantly share code, notes, and snippets.

@kenyonj
Last active October 13, 2016 14:20
Show Gist options
  • Save kenyonj/47f934b2767f051316ee7fb4969f1b19 to your computer and use it in GitHub Desktop.
Save kenyonj/47f934b2767f051316ee7fb4969f1b19 to your computer and use it in GitHub Desktop.
# What I want to do: (but it doesn't work)
def in_delivery_area_locations_scope
should_include_in_delivery_area_locations? &&
Location.where(location_order_providers: location_order_providers)
end
def location_order_providers
LocationOrderProvider.delivering_to(latitude: Float(params[:lat]),
longitude: Float(params[:lng]))
end
ERROR:
ActiveRecord::StatementInvalid:
PG::UndefinedColumn: ERROR: column locations.location_id does not exist
LINE 1: ...hant_id" = 158 AND "locations"."visible" = $2 AND "locations...
^
: SELECT "locations".* FROM (SELECT DISTINCT "locations".* FROM "locations" INNER JOIN "location_order_providers" ON "location_order_providers"."location_id" = "locat
ions"."id" WHERE (location_order_providers.fulfills_deliveries = true) AND "location_order_providers"."active" = $1) locations WHERE "locations"."merchant_id" = 158 AND "loca
tions"."visible" = $2 AND "locations"."location_id" IN (SELECT "location_order_providers"."id" FROM "location_order_providers" WHERE "location_order_providers"."active" = $3
AND "location_order_providers"."fulfills_deliveries" = $4 AND ("location_order_providers"."delivery_area" IS NOT NULL) AND (ST_Covers(location_order_providers.delivery_area,
ST_GeogFromText('POINT(-0.5 1.5)')))) ORDER BY 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((1.5 - locations.lat) * PI() / 180 / 2), 2) + COS(1.5 * PI() / 180) * COS(locations.l
at * PI() / 180) * POWER(SIN((-0.5 - locations.lng) * PI() / 180 / 2), 2))), "locations"."id" ASC LIMIT 30 OFFSET 0
# What I have to do for it to work:
def in_delivery_area_locations_scope
should_include_in_delivery_area_locations? &&
Location.where(id: location_order_providers_location_ids)
end
def location_order_providers_location_ids
LocationOrderProvider.delivering_to(latitude: Float(params[:lat]),
longitude: Float(params[:lng])).pluck(:location_id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment