【问题标题】:Implement an has_one polymorphic association with rails使用 rails 实现 has_one 多态关联
【发布时间】:2018-06-12 22:55:42
【问题描述】:

我正在使用 postgis 处理空间数据。

我有一个通用的 poi 表,其中包含名称、地址、经度和纬度等数据

我也在使用friendly_id gem

我想从 Poi 获得住宿、餐饮和城市。

一个Poi有一个住宿或一个餐饮或一个城市

我有很多适用于 Poi 的函数。

我想通过 poi 获取每种类型的数据。

我认为我必须构建一个具有 has_one 多态关联的系统...但我不知道,有什么更好的方法。

我想做什么?

通过friendly_id 调用具有不同范围(餐饮、住宿等)的Poi。

例如,我想打电话

Poi.住宿 呸。城市 Poi.餐饮

而且,对于每一个,都有一个特殊的渲染。

非常欢迎任何帮助...我是新手...而且我的英语不是更好:(

Poi 模型:

class Poi < ApplicationRecord
   extend FriendlyId
   friendly_id :name_and_zip_code, use: :slugged
   has_one :town, as: poitable
   has_one :accomodation, as poitable

城镇模式:

class Town < ApplicationRecord
 belongs_to :poitable

end

餐饮模特:

class Accomodation < ApplicationRecord
  belongs_to :poitable
end

但我有一个错误,Poi 不能同时拥有这两者.....

编辑 1

我有什么?

class Poi < ApplicationRecord
   extend FriendlyId
   friendly_id :name_and_zip_code, use: :slugged
   geocoded_by :full_address
   has_one :town, as: :poitable
   has_one :accomodation, as: :poitable

class Accomodation < ApplicationRecord
  belongs_to :poitable, polymorphic: true

class Town < ApplicationRecord
  belongs_to :poitable, polymorphic: true

在 db:migration 中:

class CreatePois < ActiveRecord::Migration[5.1]
  def change
     create_table :pois do |t|
      t.string :name
      t.st_point :lonlat, geographic: true
      t.st_point :lonlatheight, geographic: true, has_z: true
      t.belongs_to :user, index: true
      t.references :poitable, polymorphic: true, index: true
    end
  end
end

db:migration 住宿

class CreateAccomodations < ActiveRecord::Migration[5.1]
  def change
    create_table :accomodations do |t|
     t.string :genre
    end
  end
 end

db:移民城镇

class CreateTowns < ActiveRecord::Migration[5.1]
  def change
    create_table :towns do |t|
    t.string :department
   end
 end
end

在erb中,我可以创建一个Poi,但是:

  • Poitable_id:无
  • Poitable_type:无

但是,我无法创建城镇或住宿...我有一个回滚:(

我该如何解决?

【问题讨论】:

    标签: ruby-on-rails postgis polymorphic-associations has-one


    【解决方案1】:

    我认为你的模型应该是这样的:

    Poi 模型:

    class Poi < ApplicationRecord
      extend FriendlyId
      friendly_id :name_and_zip_code, use: :slugged
      has_one :town, as: :poitable
      has_one :accomodation, as: :poitable
    

    城镇模式:

    class Town < ApplicationRecord
      belongs_to :poitable, polymorphic: true
    end
    

    餐饮模特:

    class Accomodation < ApplicationRecord
      belongs_to :poitable, polymorphic: true
    end
    

    P/S:有关关联的更多信息。你可以看看这个guides

    【讨论】:

    • 感谢您的帮助...我阅读了很多指南,但他们谈论的是 has_many 多态关联,而不是 has_one。我已经更新了我的答案,如果你能看的话;)
    • 嗨@Ben,我已经为您的问题创建了一个示例项目。我的工作正常,也许你可以看看。 github.com/AlecksJohannes/rails-polymorphism。注意db migration 文件。我认为您错过了TownAccomodation 模型中的poitable_idpoitable_type 两个字段。如果这是真的,你应该运行rails g migration,然后像我的示例项目一样添加两个缺失的字段。
    • 我试了一下,是的,它有效……但它没有任何意义。所有城镇和所有住宿都具有相同的 poitable_type。我认为一个 poi 有住宿或城镇 poitable_type。也许,我需要在 poi 之前创建城镇或住宿。这不是我真正擅长的:(
    • 不,我认为您应该根据当前的poi 记录创建accomodation town。例如@poi = Poi.find(params[:id])。并且做@poi.create_town@poi.create_accomodation@poi 是你当前的poi 记录。在创建accomodationtown之后,你可以做@poi.town,看看它是否有效。
    • 但我无法在 à town 之前创建 poi.... Activerecord 需要在 end 返回错误之前有 poitable_id 和 poitable_type ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多