gpt4 book ai didi

ruby-on-rails - 在 Rails 中使用具有多态关联的 "_type"列

转载 作者:行者123 更新时间:2023-12-04 06:26:03 24 4
gpt4 key购买 nike

我有一个 Slot 模型,属于 Configuration 模型以及另一个模型:

在 db/migrate/...create_slots.rb 中:

  t.references :slottable, :polymorphic => true

在 app/models/slot.rb 中:
  belongs_to :slottable, :polymorphic => true

在 app/models/configuration.rb 中:
  has_many :slots, :as => :slottable

在 Rails 控制台中,我得到:
  ruby-1.9.2-p180 :009 > Slot.last

=> #<Slot id: 69, slottable_id: 35, slottable_type: "configuration", number: 2, usage: "1GB 667MHz DDR2 SDRAM PC2-5300 SO-DIMM", created_at: "2011-08-09 12:12:25", updated_at: "2011-08-09 12:12:25">

ruby-1.9.2-p180 :009 > Slot.last.slottable

NameError: wrong constant name configuration

我能想到的唯一解释是我误解了 slottable_type 列的使用。我有没有?

目前,在 app/models/configuration.rb 中:
slot = Slot.find_or_create_by_slottable_id_and_slottable_type(self.id, "configuration")

最佳答案

slottable_type 必须是 配置而不是 配置 .

关于ruby-on-rails - 在 Rails 中使用具有多态关联的 "_type"列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6996354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com