简体   繁体   中英

In Rails, problems reading a point type from MySQL with mysql2 gem

I'm working in a refactor from a legacy system and I'm having trouble with the "point" data type.

Inside Sequel Pro I can see the column "location" value as POINT(-22.81507676827597 -47.07767857976228)

But when I query it from the database

Mysql2::Client.new(
          host: ENV.fetch('LEGACY_DATABASE_HOST'),
          username: ENV.fetch('LEGACY_DATABASE_USERNAME'),
          password: ENV.fetch('LEGACY_DATABASE_PASSWORD'),
          database: ENV.fetch('LEGACY_DATABASE_NAME')
        ).query("SELECT * FROM local").first('location')

I get a weird result like "\\x00\\x00\\x00\\x00\\x01\\x01\\x00\\x00\\x00\\x84y\\xFF\\xDE\\xA8\\xD06\\xC0\\xE0\\xD6'_\\xF1\\x89G\\xC0", if I try to save it right away, it becomes a 0.

How is the proper way to deal with point data type in mysql with rails?

I strongly recommend that you use the activerecord-postgis-adapter gem, makes all this very simple. Although if you're set on doing it yourself, dig into that gem and see how they do the point stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM