OrestF / readymade

MIT License
9 stars 4 forks source link

Modify collection.count in collection_response method #20

Open vladknysh opened 6 months ago

vladknysh commented 6 months ago

https://github.com/OrestF/readymade/blob/e9867a088fc29d56ed5274246f34501a93c93d7c/lib/readymade/controller/serialization.rb#L26

When using gem geocoder, I encountered the issue with this line. During some long sql queries there is an issue Error: the evaluation of collection.count failed with the exception 'PG::SyntaxError: ERROR: syntax error at or near "AS" LINE 1: ....244644 - jobs.longitude) * PI() / 180 / 2), 2))) AS distanc...

SQL Query: SELECT COUNT(jobs.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((53.483959 - jobs.latitude) * PI() / 180 / 2), 2) + COS(53.483959 * PI() / 180) * COS(jobs.latitude * PI() / 180) * POWER(SIN((-2.244644 - jobs.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((jobs.longitude - -2.244644) / 57.2957795), ((jobs.latitude - 53.483959) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing) FROM "jobs" WHERE "jobs"."company_id" = $1 AND (jobs.latitude BETWEEN 53.1944954337783 AND 53.7734225662217 AND jobs.longitude BETWEEN -2.7310982941587594 AND -1.7581897058412408 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((53.483959 - jobs.latitude) * PI() / 180 / 2), 2) + COS(53.483959 * PI() / 180) * COS(jobs.latitude * PI() / 180) * POWER(SIN((-2.244644 - jobs.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 20) AND "jobs"."status" = $2 AND (published_at > '2024-04-01 07:52:54.844490') [["company_id", 1], ["status", 20]]

Our workaround was to do so:

` def collection_response(collection, *args) args.extract_options!.then do |options| render_json( serialize_collection(paginate(collection, options), options).merge(count: count(collection)), options[:status] || :ok ) end end

def count(collection) collection.is_a?(Array) ? collection.count : collection.count(:all) end `