【发布时间】:2019-03-02 06:26:03
【问题描述】:
我在我的 rails 应用程序中使用activerecord-postgis-adapter,我有一个列来存储一个点,但我不知道如何保存该点的值。
在我的架构中,我有
create_table "privacy_zones", force: :cascade do |t|
t.bigint "user_id"
t.geography "original_point", limit: {:srid=>4326, :type=>"st_point", :geographic=>true}, null: false
t.geography "random_in_range", limit: {:srid=>4326, :type=>"st_point", :geographic=>true}, null: false
t.integer "range", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_privacy_zones_on_user_id"
end
在我的控制器中我有
def create
@privacy_zone = PrivacyZone.new(
range: params[:privacy_zone][:range],
original_point: "ST_POINT(#{params[:privacy_zone][:original_point][0]}, #{params[:privacy_zone][:original_point][1]})",
random_in_range: "ST_POINT(#{params[:privacy_zone][:random_in_range][0]}, #{params[:privacy_zone][:random_in_range][1]})",
user: current_user
)
p @privacy_zone
end
当我发布到它时,我会在服务器日志中看到这一点
Started POST "/profile/privacy_zone" for 127.0.0.1 at 2019-03-02 16:47:24 +1030
Processing by PrivacyZoneController#create as HTML
Parameters: {"privacy_zone"=>{"original_point"=>[35.87006446264988, 107.75390625], "random_in_range"=>[35.87185877011052, 107.75633485018554], "range"=>400}}
User Load (4.4ms) SELECT "users".* FROM "users" WHERE "users"."uid" = $1 LIMIT $2 [["uid", "test@example.com"], ["LIMIT", 1]]
#<PrivacyZone id: nil, user_id: 1, original_point: nil, random_in_range: nil, range: 400, created_at: nil, updated_at: nil>
original_point 和 random_in_range 为 nil,因此记录不会保存。将数据插入 st_point 列的正确方法是什么?
【问题讨论】:
标签: ruby-on-rails rails-activerecord postgis