Of course, Here's my situation,
I am using spatial-adapter for a rails 3.1 app [ spatial-adapter
worked just fine for a rails 3.0 app ], I followed the usual steps and
created a geometry column with migrations and afterwards when I tried
to create a new record in the database I got the following error:
ERROR: parse error - invalid geometry
HINT: You must specify a valid OGC WKT geometry type such as POINT,
LINESTRING or POLYGON
I googled a bit and this link came up ( which is one month old and
didn't answer my question )
ruby - Rails - PostGIS + postgis_adapter Geometry Problem - Stack Overflow
So I jumped into the rails code [ for the first time ], and found/
think that type_cast is the problem. I've patched the spatial-adapter
code for project [ Actually I've change Quoting::type_cast through
monkey patching, but It's easy to do it the other way. ]
The type_cast method on the column is the wrong method to use in this
case. Let's explore why that is the case.
If we look at the `type_cast` method on the column object in Rails,
we'll see that it converts a type that comes *from* the database in to a
type that we want to use in ruby land:
However, in this case, we're trying to convert Ruby object types in to a
data type that the prepared statement can understand. Namely, strings,
integers and possibly booleans. We have a default set of conversion
types that work across all databases in the abstract adapter:
And we have a specialization in the postgresql subclass:
Recall that we are attempting to convert to a type that can be inserted
in to the database. Then take a look at the `type_cast` implementation
in the postgis adapter:
It is converting to a GeoRuby (which seems to be no longer maintained
according to this site: http://georuby.rubyforge.org/) Geometry class.
Postgres doesn't know what that type is, so it would be impossible to
insert it to your database.
The best method for dealing with this is if the gem provided a subclass
of the postgresql adapter that adds PostGIS functionality. Which is
exactly what this one does:
Hope that helps!
Btw, I didn't know about the prepared statement, It's a grandiose
feature to have.
Yes. I'm happy to be using them!