好的,所以这看起来像是一个新手问,但是当我创建一个我的模型实例时let
,由于某种原因,它上面的所有 getter 方法都返回 nil。这是我的例子:
模型:
class Parcel < ActiveRecord::Base
extend UUIDHelper
serialize :address, ActiveRecord::Coders::Hstore
self.rgeo_factory_generator = RGeo::Geographic.spherical_factory srid: 3785
attr_accessible :address, :area_poly, :id, :lonlat, :municipal_id
end
规格:
require 'spec_helper'
require 'street_address'
describe Parcel do
let(:geo_factory) { RGeo::Geographic.spherical_factory(srid: 3785)}
let(:point1) {geo_factory.point(-72.9229165, 41.3096987)}
let(:point2) {geo_factory.point(-72.9229169, 41.3096987)}
let(:point3) {geo_factory.point(-72.9229169, 41.3096983)}
let(:point4) {geo_factory.point(-72.9229165, 41.3096983)}
let(:line_string) {geo_factory.line_string([point1,point2,point3,point4])}
let(:polygon) {geo_factory.polygon(line_string)}
let(:parcel) { Parcel.create(
municipal_id: 'abc123',
area_poly: polygon,
#lonlat: polygon.centroid,
address: StreetAddress::US.parse('55 Whitney Ave New Haven, CT 06510').as_json
)}
it "#municipal_id should not be nil" do
parcel.municipal_id.nil? should_not be_true
end
end
(#longlat 属性被注释掉,因为.centroid
在 RGeo 中对球形定义的多边形调用该方法是不行的。在我的待办事项列表中获得正确的投影与这个问题无关)并且规范失败像这样:
expected: non-true value
got: #<Parcel id: nil, municipal_id: nil, address: {}, created_at: nil, updated_at: nil, area_poly: nil, lonlat: nil>
FWIW,这是 Rspec 日志:
Connecting to database specified by database.yml
(0.1ms) BEGIN
(7.2ms) SELECT * FROM geometry_columns WHERE f_table_name='parcels'
(0.1ms) SAVEPOINT active_record_1
SQL (4.2ms) INSERT INTO "parcels" ("address", "area_poly", "created_at", "id", "lonlat", "municipal_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) [["address", "\"number\"=>\"55\",\"street\"=>\"Whitney\",\"street_type\"=>\"Ave\",\"unit\"=>NULL,\"unit_prefix\"=>NULL,\"suffix\"=>NULL,\"prefix\"=>NULL,\"city\"=>\"New Haven\",\"state\"=>\"CT\",\"postal_code\"=>\"06510\",\"postal_code_ext\"=>NULL"], ["area_poly", #<RGeo::Geographic::SphericalPolygonImpl:0x80b773e0 "POLYGON ((-72.9229165 41.3096987, -72.9229169 41.3096987, -72.9229169 41.3096983, -72.9229165 41.3096983, -72.9229165 41.3096987))">], ["created_at", Mon, 02 Jun 2014 11:59:51 UTC +00:00], ["id", nil], ["lonlat", nil], ["municipal_id", "abc123"], ["updated_at", Mon, 02 Jun 2014 11:59:51 UTC +00:00]]
(0.1ms) RELEASE SAVEPOINT active_record_1
(0.1ms) ROLLBACK
作为参考,我可以Parcel.create
在 Rails 控制台中成功运行,并获取实例变量以返回非零值
我正在运行 rails 3.2,使用带有 postgis 2 的 postgres 9.3,并且严重依赖于 activerecord-postgis-adaptor、RGeo、activerecord-postgres-hstore 等。
有什么想法吗?