RustComet
RustComet

Reputation: 567

Postgis geography column throwing exception on save

First things first:

Ruby: 2.2.3

Rails: 4.2.5

I have a model in my database (lets call it destination) that has a coordinates column, which was created with a migration like so:

add_column :destinations, :coordinates, :st_point, geographic: true

I have a callback setup to set the coordinates from a lat and lon field that are passed in:

attr_accessor :lat, :lon

before_validation :set_coordinates

private

def set_coordinates
  if lat.present? && lon.present?
    self.coordinates = "POINT(#{lon} #{lat})"
  end
end

which appears to be working correctly. Given that I have passed in a lon and lat it returns the coordinates as follows:

coordinates: #<RGeo::Geographic::SphericalPointImpl:0x3ff3b1fa767c "POINT (144.96975 -37.810177)">

However when I try and call save on the model (with or without a !) I am met with the following error:

destination.save
fatal: exception reentered
from /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/transactions.rb:304:in `rescue in rollback_active_record_state!'

if I don't try and set this column then the model saves correctly and it's happy times ahead. Unfortunately this is a rather important part of the models functionality…

THINGS I HAVE TRIED SO FAR:

Setting coordinates like so:

self.coordinates = RGeo::Geographic.spherical_factory(:srid => 4326).point(lon,lat)`

Setting the default factory:

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = RGeo::Geographic.spherical_factory(srid: 4326)
end

I have run all the checks to make sure that I have Postgis working in my database, and have the latest version of activerecord-postgis-adapter in the gemfile and installed.

Just hoping somebody else out there might have run into the same thing and has an idea of what is causing it.

UPDATE:

After playing around a little more (removing all validations and just setting everything in the console) I managed to get the following error instead:

[5] pry(main)> destination.save

NoMethodError:
undefined method `reject!' for nil:NilClass 
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:187:in   `rescue in within_new_transaction'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:201:in `within_new_transaction'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in `transaction'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/transactions.rb:291:in `save!'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
# ./spec/spec_helper.rb:136:in `is_expected_to_have_a_valid_factory'
# ./spec/models/issue_spec.rb:25:in `block (3 levels) in <top (required)>'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
# -e:1:in `<main>'
# ------------------
# --- Caused by: ---
# fatal:
#   exception reentered
#

Not sure if that actually helps. But the more info the better I guess

UPDATE 2:

Log output (nothing helpful here looking at it now):

  SQL (4.7ms)  INSERT INTO "destinations" ("customer_id", "profession_id", "address", "credit_card_id", "coordinates", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["customer_id", "ca5213d1-bdd3-4090-8f5a-11524d9c4dfa"], ["profession_id", "f18d3c54-9315-4415-a832-34299cfa8c5b"], ["address", "45756 Thalia Spurs, O'Connor ACT 6168"], ["credit_card_id", "a6f38078-cce0-4804-8c38-6bca24948c16"], ["coordinates", "0020000001000010e640621f083126e979c042e7b3e1437c57"], ["created_at", "2015-12-17 03:20:51.715434"], ["updated_at", "2015-12-17 03:20:51.715434"]]
  (0.3ms)  ROLLBACK TO SAVEPOINT active_record_2

Upvotes: 1

Views: 238

Answers (1)

RustComet
RustComet

Reputation: 567

SOLVED

After a lot of digging a came across this issue on the activerecord-postgis-adapter gem repo.

It seems that having the meta_request gem installed causes this behaviour, even just in the :development group.

Going to leave this up in case anybody else comes across this weird and obscure issue.

What a way to end the year… :D

Upvotes: 1

Related Questions