【发布时间】:2022-11-08 06:26:34
【问题描述】:
我有 2 个模型:一个拥有_many 工具的用户(属于_to 用户)。 每个用户都有一个地址。当用户列出新工具时,该地址将与其所有者具有相同的地址。 在一般情况下,我只需调用 tool.user.address 并获取其地址。 但是,我正在尝试对工具使用地理编码,它既不接受这种类型的调用,也不接受“委托:地址,到::用户”。
我想知道在用户创建新工具时是否有办法从用户那里传递地址,以及如何做到这一点。 此外,如果/当用户更改其地址时,是否可以自动更新工具的地址? 谢谢!
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.string "name"
t.string "address"
t.string "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "latitude"
t.float "longitude"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
create_table "tools", force: :cascade do |t|
t.string "name"
t.float "price"
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "rating"
t.string "address", :user
t.float "latitude"
t.float "longitude"
t.index ["user_id"], name: "index_tools_on_user_id"
end
【问题讨论】:
-
错误消息告诉您您的工具表缺少存储其位置所需的纬度和经度列。如果您使用 Postgres PostGIS 扩展和 ActiveRecord 适配器,您可以选择使用其本机 st_point 地理类型而不是两列。 pganalyze.com/blog/postgis-rails-geocoder
-
“此外,如果/当用户更改其地址时,是否可以自动更新工具的地址?” - 是的,没有。您可以使用回调自动执行此操作,但从长远来看,明确执行此操作可能会更好 - 例如在服务对象中。
标签: ruby-on-rails