ActiveRecord 将字段设置为 SQL 表达式

发布于 2024-12-14 14:25:30 字数 488 浏览 3 评论 0原文

我正在使用 Ruby 1.8.7 和 Rails 2.3.8,并且有许多由纬度和经度表示的位置。我正在使用 SQLite SpatiaLite 扩展将坐标转换为众所周知的二进制 (WKB) 格式。我关心的是如何让 ActiveRecord 执行插入或更新,调用适当的 SpatiaLite 转换方法。我想做这样的事情:

obj.geometry = "AsBinary(MakePoint(4, 51))"
obj.save
# ActiveRecord would now do something like:
# INSERT INTO objects (geometry) VALUES (AsBinary(MakePoint(4, 51)))

Is this possible with ActiveRecord?

我已经尝试过 GeoRuby,但虽然它能够正确读取 WKB blob,但无法将它们保存为 SpatiaLite 所用的相同格式。

谢谢!

I am using Ruby 1.8.7 and Rails 2.3.8 and have a number of locations represented by a latitude and a longitude. I am using the SQLite SpatiaLite extension to convert the coordinates to the well-known binary (WKB) format. My concern is how to have ActiveRecord perform the insert or update, calling the appropriate SpatiaLite conversion methods. I am looking to do something like this:

obj.geometry = "AsBinary(MakePoint(4, 51))"
obj.save
# ActiveRecord would now do something like:
# INSERT INTO objects (geometry) VALUES (AsBinary(MakePoint(4, 51)))

Is this possible with ActiveRecord?

I have already tried GeoRuby but while its able to properly read WKB blobs, it is unable to save them to the same format SpatiaLite does.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-12-21 14:25:30

我还没有使用GeoRuby,但是对于空间数据,我使用了thinking-sphinx和@geodist函数,使用工具非常好。

您可以阅读有关它的一些资源:

I didn't use GeoRuby yet, but for spatial data I used thinking-sphinx with @geodist function, and it's quite nice to use tool.

Some resources you can read about it:

烟酉 2024-12-21 14:25:30

经过几天的搜索并没有找到解决方案后,我决定简单地利用原始连接来执行实际的插入:

rc = ActiveRecord::Base.connection.raw_connection
rc.execute("INSERT INTO objects (geometry)
            VALUES (MakePoint(#{obj.lng}, #{obj.lat}))")

After days of searching and coming up with no solution, I decided to simply leverage the raw connection to perform the actual insert:

rc = ActiveRecord::Base.connection.raw_connection
rc.execute("INSERT INTO objects (geometry)
            VALUES (MakePoint(#{obj.lng}, #{obj.lat}))")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文