0

I'm using GeoCoder for location based queries. The .Near method from GeoCoder adds a Distance & Bearing column to the select clause which is useful for where and order clauses.

Those columns are fine, but it also adds the current model.* which I'm trying to avoid with a specific column list in the select statement.

The following query and image show the extra additions, the red is what I wish to remove, the yellow is added by GeoCoder, but I'm ok with that.

sql = Shift.scoped
      .select("CAST('#{region.name}' AS character(50)) as region, staffrooms.name as staffroom_name, staffrooms.staffroom_type, shifts.id, shifts.title, shifts.shift_type, shifts.status, shifts.location, shifts.uuid")
      .visible
      .jobs
      .joins(:posted_shortlists)
      .near([region.latitude, region.longitude], 50)

    sql = sql.where("staffrooms.staffroom_type = ?", staffroom_type) if staffroom_type
    sql = sql.reorder("staffrooms.name")

enter image description here

How do I remove the shifts.* from the query, my current technique is here, but it seems a bit dodgy to me and does not allow the query to remain as a Relation

sql = sql.gsub("shifts.*, ", "")
David Cruwys
  • 6,262
  • 12
  • 45
  • 91

1 Answers1

0

I found my answer here

My solution was to clear the select and reset it anytime after the .Near method using the following:

  .except(:select)
  .select("CAST('#{region.name}' AS character(50)) as region, CAST('#{region.id}' AS Integer) as region_id, staffrooms.id as staffroom_id, staffrooms.name as staffroom_name, staffrooms.staffroom_type, shifts.id, shifts.title, shifts.shift_type, shifts.status, shifts.location, shifts.uuid, shifts.comments")

Full example

sql = Shift.scoped
      .visible
      .jobs
      .joins(:posted_shortlists)
      .near([region.latitude, region.longitude], 50)
      .except(:select)
      .select("CAST('#{region.name}' AS character(50)) as region, CAST('#{region.id}' AS Integer) as region_id, staffrooms.id as staffroom_id, staffrooms.name as staffroom_name, staffrooms.staffroom_type, shifts.id, shifts.title, shifts.shift_type, shifts.status, shifts.location, shifts.uuid, shifts.comments")
Community
  • 1
  • 1
David Cruwys
  • 6,262
  • 12
  • 45
  • 91