記事はいろいろでてくるんですが似た名前のジェムがいろいろあって
どのジェムの記事なのかよくわからず
いろいろ試してみてるのですがどれもうまくいきません
GeoRuby というので geometry カラムのマイグレーションは通ったのですが
Point タイプを書き込む方法がわからずジェムのサイトがみつかりません
geo-ruby というジェムは active record の 5 以上が必要といわれて
いま 4.2.3 なのですぐにはアップデートできず
georuby というのもあるんですがタイプを扱うだけで
MySQL のカラムを扱うには別のジェムが必要なようです
activerecord-mysqlspatial-adapter
もためしたんですが bundle install は通ったものの
Rails を起動すると
/usr/local/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:84:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'rgeo-activerecord'. (Bundler::GemRequireError) Gem Load Error is: undefined method `attribute_types_cached_by_default' for ActiveRecord::Base:Class Backtrace for gem load error is:
というエラーが出て動きません
Rails 4.2.3 から MySQL の geometry 型を扱えてる方いましたらジェムを教えていただけないでしょうか
geometry を使った検索自体は別のフレームワークでやるので
Rails からはあるカラムに lat, lng => Point の出し入れができるだけでいいのですが…
###追記:
よく検索にひっかかる armg と activerecord-mysql2spatial-adapter
がいずれも Rails 4.2.3 で使えません
In Gemfile: armg was resolved to 0.7.1, which depends on activerecord (~> 5) rails (= 4.2.3) was resolved to 4.2.3, which depends on activerecord (= 4.2.3)
activerecord-mysql2spatial-adapter was resolved to 0.5.2, which depends on activerecord (< 4.2, >= 4.0) rails (= 4.2.3) was resolved to 4.2.3, which depends on activerecord (= 4.2.3)
追記:
https://github.com/fjl82/activerecord-mysql2spatial-adapter/tree/activerecord-4.2
4.2 用のブランチがあったので Gemfile で指定してインストールするとインストールできました
user.rb に
# By default, use the GEOS implementation for spatial columns. self.rgeo_factory_generator = RGeo::Geos.method(:factory) # But use a geographic implementation for the :latlon column. set_rgeo_factory_for_column(:user_location_center, RGeo::Geographic.spherical_factory)
とかいてみたところエラーが出なかったので使えてるんだと思うのですが使い方がわかりません
以下のようなマイグレーションで user モデルの location というカラムを point type にしたのですが
create_table :users do |t| t.column :location, :point, null: false
user.rb で
before_save :set_columns def set_columns self.location = "POINT(137, 39)" puts self.location end
とポイント型のデータを挿入するテストコードをかいてみたのですが
puts の結果も SQL のログも値が0になってしまいます
0 Mysql2::Error: Cannot get geometry object from data you send to the GEOMETRY field: INSERT INTO `users` (`location`, `created_at`, `updated_at`) VALUES (0, '2019-03-11 04:49:52', '2019-03-11 04:49:52')):
activerecord-mysql2spatial-adapter
の使い方がわかる方いらっしゃったら教えていただけないでしょうか
あなたの回答
tips
プレビュー