Ok, so this may seem like a total newbie ask, but when I create an instance of my model with let
, for some reason all of the getter methods on it return nil. Here's my example:
Model:
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
Spec:
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
(the #longlat attribute is commented out because calling the .centroid
method on a spherically defined polygon in RGeo is a no-no. Getting the right projection, while on my list of to-dos, is not relevant to this question)
And the spec fails like so:
expected: non-true value
got: #<Parcel id: nil, municipal_id: nil, address: {}, created_at: nil, updated_at: nil, area_poly: nil, lonlat: nil>
FWIW, here's the Rspec log:
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
For reference, I can successfully run Parcel.create
in a Rails console, and get instance variables to return non-nil values
I'm running rails 3.2, using postgres 9.3 with postgis 2, and heavily relying on activerecord-postgis-adaptor, RGeo, activerecord-postgres-hstore, among other gems.
Any thoughts?