我想实现一个简单的多态。我的目标是用户、组和其他模型可以共享一个 ID 池,以便所有内容都代表一个GlobalObject
. 我建立了这样的东西:
global_object.rb
生成者$ rails generate scaffold GlobalObject mode:integer
:class GlobalObject < ActiveRecord::Base attr_accessible :mode has_many :identifyable, :polymorphic => true, :dependent => :destroy end
user.rb
生成者$ rails generate scaffold User login password
:class User < ActiveRecord::Base attr_accessible :login, :password, :salt belongs_to :global_object, :as => :permittable end
组模型将遵循非常相似的结构。我没有修改 db 文件夹中的迁移文件,只是执行了$ rake db:migrate
.
20131102224115_create_global_objects.rb
:class CreateGlobalObjects < ActiveRecord::Migration def change create_table :global_objects do |t| t.integer :mode t.timestamps end end end
20131102224332_create_users.rb
:class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :login t.string :password t.timestamps end end end
当我执行$ rake
时,它显示了两个令人印象深刻的痕迹,这些痕迹始于:
(...)hash/keys.rb:51:in `block in assert_valid_keys': Unknown key: polymorphic (ArgumentError)
from /Users(...)
...
(...)hash/keys.rb:51:in `block in assert_valid_keys': Unknown key: polymorphic (ArgumentError)
from /Users(...)
...
当我删除:polymorphic => true
global_object.rb 中的时,会发生相同的错误,但Unknown key: polymorphic
它不是 ,而是Unknown key: as
。怎么了?