我正在尝试定义has_many_polymorphs plugin
从单个父级到相同子级的多个多态关系 ()。
笔记有很多查看者
笔记有很多编辑者
查看者可以是用户或组
编辑者可以是用户或组权限是具有, , , ,字段
的关联模型note_id
viewer_id
viewer_type
editor_id
editor_type
只要我在 Note 模型中只定义了一个 has_many_polymorphs 关系,一切都会按预期进行
class User < ActiveRecord::Base; end
class Group < ActiveRecord::Base; end
class Note < ActiveRecord::Base
has_many_polymorphs :viewers, :through => :permissions, :from => [:users, :groups]
end
class Permission < ActiveRecord::Base
belongs_to :note
belongs_to :viewer, :polymorphic => true
end
Note.first.viewers << User.first # => [#<User id: 1, ....>]
Note.first.viewers << Group.first # => [#<User id: 1, ....>, #<Group ...>]
Note.first.viewers.first # => #<User ....>
Note.first.viewers.second # => #<Group ....>
现在,当我添加第二个关系时,问题开始出现
class Note < ActiveRecord::Base
has_many_polymorphs :viewers, :through => :permissions, :from => [:users, :groups]
has_many_polymorphs :editors, :through => :permissions, :from => [:users, :groups]
end
class Permission < ActiveRecord::Base
belongs_to :note
belongs_to :viewer, :polymorphic => true
belongs_to :editor, :polymorphic => true
end
Note.first.viewers << User.first # => [#<User id: ....>]
# >>>>>>>>
Note.first.editors << User.first
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.constantize
... vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/base.rb:18:in `instantiate'
我已经尝试改进定义,has_many_polymorphs
但没有奏效。甚至没有用于ViewPermission < Permission
和的 STI 模型EditPermission < Permission
。
感谢任何想法/解决方法/问题指针。
导轨 2.3.0