ruby-on-rails – 在Rails中使用带有多态关联的“_type”列
发布时间:2020-12-17 04:17:00 所属栏目:百科 来源:网络整理
导读:我有一个Slot模型,属于Configuration模型以及另一个模型: 在db / migrate / … create_slots.rb中: t.references :slottable,:polymorphic = true 在app / models / slot.rb中: belongs_to :slottable,:polymorphic = true 在app / models / configuratio
我有一个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必须是Configuration而不是配置.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |