Mongoid关系多态关联

时间:2010-09-29 23:23:19

标签: mongoid

有没有人知道如何在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。当我将reference更改为embed时,这一切都按照Mongoid's documentation中的说明进行操作,但我需要reference

谢谢!

3 个答案:

答案 0 :(得分:20)

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

现在还有一个多态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

答案 1 :(得分:4)

来自Mongoid Google Group似乎不支持此功能。这是我找到的newest relevant post

无论如何,这并不难以手动实现。这是我的多态链接,名为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

答案 2 :(得分:1)

Rails 4 +

以下是如何在 Mongoid 中为Comment模型实现多态关联,该模型可以同时属于PostEvent模型。

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+中,您可以使用Concern模式并在commentable中创建一个名为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
相关问题