一个模型中有多个 has_many_polymorphs

发布于 2024-07-24 07:47:49 字数 1980 浏览 5 评论 0原文

我正在尝试定义从单个父级到相同子级的多个多态关系(has_many_polymorphs 插件)。

注释有很多查看者
Note 有很多编辑器
查看者可以是用户或组
编辑者可以是用户或组
权限是与 note_idviewer_idviewer_typeeditor_ideditor_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 STI 模型也不行。 权限EditPermission < 权限

任何想法/解决方法/问题指针表示赞赏。

导轨2.3.0

I'm trying to define multiple polymorphic relations (has_many_polymorphs plugin) from a single parent to same children.

Note has many viewers
Note has many editors
Viewers could be either Users or Groups
Editors could be either Users or Groups
Permission is the association model with note_id, viewer_id, viewer_type, editor_id, editor_type fields

Everything works out as expect as long as I have only one has_many_polymorphs relation defined in Note model

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 ....>

Now, problems start to appear when I add the second relation

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'

I've tried refining the definition of has_many_polymorphs but it didn't work. Not even with an STI model for ViewPermission < Permission, and EditPermission < Permission.

Any thoughts / workarounds / issue pointers are appreciated.

Rails 2.3.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2024-07-31 07:47:49

您不需要添加

has_many :permissions

到您的注释中吗?
供参考。 我使用过一次 has_many_polymorphs 但后来放弃了它,它没有按预期工作。

您可以发布您用于权限的架构吗? 我的猜测是问题的根源就在那里,您需要在架构中有多个类型、id 对,因为定义中有两个不同的 belongs_to

编辑:

我看到你也在 github 上发布了这个问题。 不确定您是否尝试使用双面多态性。 你可能已经......就像我说的,我对这个插件印象不深,因为它在我使用它时带来了一些不稳定。

== Double-sided polymorphism

Double-sided relationships are defined on the join model:

      class Devouring < ActiveRecord::Base
        belongs_to :guest, :polymorphic => true
        belongs_to :eaten, :polymorphic => true

        acts_as_double_polymorphic_join(
          :guests =>[:dogs, :cats], 
          :eatens => [:cats, :birds]
        )       
      end


Now, dogs and cats can eat birds and cats. Birds can't eat anything (they aren't <tt>guests</tt>) and dogs can't be 
eaten by anything (since they aren't <tt>eatens</tt>). The keys stand for what the models are, not what they do. 

Dont you need to add

has_many :permissions

to your Note.
FYI. I used has_many_polymorphs once but then dropped it, it wasn't working as expected.

Can you post the schema that you are using for Permission? My guess is the root of the problem lies there, you need to have multiple type, id pairs in the schema since you have two different belongs_to in the definition.

Edit:

I see you have posted the question on github as well. Not sure if you tried using the Double sided polymorphism. You probably have... like I said, I was not impressed by this plugin, as it brought in some instability when I used it.

== Double-sided polymorphism

Double-sided relationships are defined on the join model:

      class Devouring < ActiveRecord::Base
        belongs_to :guest, :polymorphic => true
        belongs_to :eaten, :polymorphic => true

        acts_as_double_polymorphic_join(
          :guests =>[:dogs, :cats], 
          :eatens => [:cats, :birds]
        )       
      end


Now, dogs and cats can eat birds and cats. Birds can't eat anything (they aren't <tt>guests</tt>) and dogs can't be 
eaten by anything (since they aren't <tt>eatens</tt>). The keys stand for what the models are, not what they do. 
风蛊 2024-07-31 07:47:49

@Tamer

我遇到了同样的错误。 问题是 has_many_polymorphs 使用质量关联在连接表中创建记录,但失败了。 我将 attr_accessible :note_id:editor_id:editor_type 添加到我的 Permission 类中,然后它就起作用了。 (注意:我用你的模型名称替换了我的模型名称。)

我没有对此进行太多研究,但我很好奇是否有办法改变这种行为。 我对这个框架相当陌生,让任何敏感的东西(比如订单支付关联)被大规模分配似乎就像是在搬起石头砸自己的脚。 请告诉我这是否解决了您的问题,以及您是否还发现了其他问题。

最好的,
史蒂夫

@Tamer

I was getting the same error. The problem was that has_many_polymorphs creates the record in the join table using mass association and was failing. I added attr_accessible :note_id, :editor_id, and :editor_type to my Permission class and it worked afterwards. (Note: I substituted your model names for mine.)

I haven't looked too much into it, but I'd be curious if there's a way to alter this behavior. I'm fairly new to this framework and letting anything sensitive (like an Order-Payment association) be mass-assigned seems like asking to shoot myself in the foot. Let me know if this fixed your problem, and if you figure anything else out.

Best,
Steve

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文