activerecord-postgis-adapter default value

101 views
Skip to first unread message

piet...@gmail.com

unread,
Aug 24, 2013, 9:10:03 AM8/24/13
to rgeo-...@googlegroups.com
Does anyone know how to set a default value for a point column using activerecord-postgis-adapter? Any default that I add to my migration seems totally ineffective, e.g.

  add_column :places, :location, :point, geographic: true, default: RGeo::Geographic.spherical_factory.point(10.159607, 48.881721)

Thanks.

piet...@gmail.com

unread,
Aug 24, 2013, 12:07:11 PM8/24/13
to rgeo-...@googlegroups.com
So it appears that the only way to do it is through SQL, e.g.

  ALTER TABLE places ALTER COLUMN location SET DEFAULT ST_GeomFromText('POINT(10.159607 48.881721)', 4326)

Is that correct?

In any event, now I'm disappointed to find that even when set in the database, the default is not being used to initialize the attribute with an appropriate Rgeo object, i.e.

  Place.new.location => nil # As opposed to a point at (10.159607, 48.881721)

Anybody know anything about this? Can it be fixed somehow so that the spatial attributes act like regular ones and take their defaults from the schema?

Thanks.

Peter Schmitt

unread,
Mar 22, 2017, 4:57:14 PM3/22/17
to RGeo-Users
On Saturday, August 24, 2013 at 10:07:11 AM UTC-6, piet...@gmail.com wrote:
So it appears that the only way to do it is through SQL, e.g.

  ALTER TABLE places ALTER COLUMN location SET DEFAULT ST_GeomFromText('POINT(10.159607 48.881721)', 4326)

Is that correct?


I just ran into a similar problem.  I wound up using your solution to execute raw SQL to set the default. 

I had originally done this:

    f = RGeo::Geos.factory(srid: 4326)
    empty_polygon = f.polygon(f.line_string([]))
    add_column :solutions, :visible_geom, :geometry, srid: 4326, default: empty_polygon

but reverted back when my db/schema.rb contained a line like this:

    t.geometry "visible_geom", limit: {:srid=>4326, :type=>"geometry"}, default: #<RGeo::Geos::CAPIPolygonImpl:0x3a2d3a4 "POLYGON EMPTY">

Thee memory address would change every time I ran the migration.  Either way is not a great solution as I do not think I could safely create a new database from schema.rb without running the migrations.

Anyone know if there's a better way to set the default value on a geometry column with activerecord-postgis-adapter?

 
In any event, now I'm disappointed to find that even when set in the database, the default is not being used to initialize the attribute with an appropriate Rgeo object, i.e.

  Place.new.location => nil # As opposed to a point at (10.159607, 48.881721)

Anybody know anything about this? Can it be fixed somehow so that the spatial attributes act like regular ones and take their defaults from the schema?


It makes sense that Place.new.location == nil:  Place.new doesn't talk to the database.  You probably need something like this in your model:

class User < ActiveRecord::Base
  after_initialize do
    location =  RGeo::Geos.factory(srid: 4326).point([10.159607, 48.881721])
  end

  # The rest of your model definition...
end
Reply all
Reply to author
Forward
0 new messages