7
我有一个插槽模式“_type”栏目,属于配置模型以及另一种模式:使用多态协会的Rails
以dB /迁移/ ... create_slots.rb:
t.references :slottable, :polymorphic => true
在应用程序/模型/ slot.rb:
belongs_to :slottable, :polymorphic => true
在应用程序/模型/ configuration.rb:
has_many :slots, :as => :slottable
在轨控制台我得到:
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列。我有没有?
目前,在应用程序/模型/ configuration.rb:
slot = Slot.find_or_create_by_slottable_id_and_slottable_type(self.id, "configuration")
哇!我永远不会发现这一点。似乎很奇怪在数据库中需要大写字母。 –
这并不奇怪,它是类名,类名是用大写字母写的。 –