5

有谁知道如何进行多态关联,Mongoid因为它有利于关系而不是嵌入。

例如这是我的Assignment模型:

class Assignment
  include Mongoid::Document
  include Mongoid::Timestamps

  field :user
  field :due_at, :type => Time

  referenced_in :assignable, :inverse_of => :assignment
end

可以与多个模型具有多态关系:

class Project
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, :type => String

  references_many :assignments
end

这会引发一个错误,提示未知常量 Assignable。当我将 更改为referenceembed,这一切都按照Mongoid文档中的说明工作,但我需要它是reference.

谢谢!

4

3 回答 3

21

回答一个古老的帖子,但有人可能会觉得它很有用。

现在还有一个多态belongs_to

class Action                                                                                                                           
  include Mongoid::Document                                                                                                            
  include Mongoid::Timestamps::Created                                                                                                 

  field :action, type: Symbol

  belongs_to :subject, :polymorphic => true                                                                                            
end

class User                                                                                                                             
  include Mongoid::Document                                                                                                            
  include Mongoid::Timestamps                                                                                                          
  field :username, type: String
  has_many :actions, :as => :subject   
end

class Company                                                                                                                          
  include Mongoid::Document                                                                                                            
  include Mongoid::Timestamps                                                                                                          

  field :name, type: String                                                                                                            

  has_many :actions, :as => :subject
end
于 2012-03-05T07:38:10.797 回答
4

从 Mongoid Google Group 看来,这似乎不受支持。这是我找到的最新相关帖子。

无论如何,这并不难手动实现。这是我的名为 Subject 的多态链接。

实现关系的逆部分可能会稍微复杂一些,尤其是因为您需要跨多个类使用相同的代码。

class Notification
  include Mongoid::Document
  include Mongoid::Timestamps

  field :type, :type => String
  field :subject_type, :type => String
  field :subject_id, :type => BSON::ObjectId

  referenced_in :sender, :class_name => "User", :inverse_of => :sent_notifications
  referenced_in :recipient, :class_name => "User", :inverse_of => :received_notifications

  def subject
    @subject ||= if subject_type && subject_id
      subject_type.constantize.find(subject_id)
    end
  end

  def subject=(subject)
    self.subject_type = subject.class.name
    self.subject_id   = subject.id
  end
end
于 2010-12-27T12:56:02.217 回答
2

导轨 4+

以下是在Mongoid中为既属于 a又属于模型的模型实现多态关联的方法。CommentPostEvent

Comment型号:

class Comment
  include Mongoid::Document
  belongs_to :commentable, polymorphic: true

  # ...
end

Post/Event型号:

class Post
  include Mongoid::Document
  has_many :comments, as: :commentable

  # ...
end

使用问题:

在 Rails 4+ 中,您可以使用关注模式并创建一个名为commentablein的新模块app/models/concerns

module Commentable
  extend ActiveSupport::Concern

  included do
    has_many :comments, as: :commentable
  end
end

在你的模型中只有include这个模块:

class Post
  include Mongoid::Document
  include Commentable

  # ...
end
于 2017-03-03T01:02:17.000 回答