Postgis地理列在保存时抛出异常

时间:2015-12-17 03:36:52

标签: ruby-on-rails ruby postgresql activerecord postgis

首先要做的事情:

Ruby: 2.2.3

Rails: 4.2.5

我的数据库中有一个模型(让我们称之为目的地),它有一个coordinates列,它是通过这样的迁移创建的:

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

我有一个回调设置来设置传入的latlon字段的坐标:

attr_accessor :lat, :lon

before_validation :set_coordinates

private

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

似乎工作正常。鉴于我已通过lonlat,它返回coordinates,如下所示:

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

但是当我尝试在模型上调用save时(有或没有!),我遇到以下错误:

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!'

如果我没有尝试设置此列,那么模型会正确保存,并且它是未来的快乐时光。不幸的是,这是模型功能中相当重要的一部分......

我已经尝试过的事情:

设置坐标如下:

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

设置默认工厂:

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

我已经运行了所有检查,以确保我在我的数据库中使用Postgis,并在gemfile中安装了最新版本的activerecord-postgis-adapter并安装。

只是希望那里的其他人可能遇到同样的事情,并且知道造成它的原因。

更新

在玩了一点之后(删除所有验证并只在控制台中设置所有内容)我设法得到以下错误:

[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
#

不确定这是否真的有帮助。但是我猜的信息越多越好

更新2:

日志输出(现在没有任何帮助):

  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

1 个答案:

答案 0 :(得分:1)

已解决

经过大量挖掘后,在activerecord-postgis-adapter gem repo上遇到了http://laravel.com/docs/5.1/queues#job-class-structure

似乎安装了this issue gem会导致此行为,即使只是在:development组中也是如此。

如果其他任何人遇到这个奇怪而模糊的问题,请不要这样做。

结束这一年的方式......:D