Frage

Wer weiß, wie eine polymorphe Vereinigung in Mongoid zu tun, die von der relationalen Gunst ist aber nicht der Einbettung einer.

Zum Beispiel dies ist mein Assignment Modell:

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

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

  referenced_in :assignable, :inverse_of => :assignment
end

, die eine polymorphe Beziehung mit mehreren Modellen haben:

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

  field :name, :type => String

  references_many :assignments
end

Dies wirft eine Fehlermeldung, unbekannte Konstante zuweisbare. Als ich die reference zu embed ändern, das alles funktioniert wie dokumentiert in Mongoid Dokumentation , aber ich brauche es reference zu sein.

Danke!

War es hilfreich?

Lösung

Von Mongoid Google-Gruppe sieht es so aus, wird nicht unterstützt. Hier ist der neueste relevante Beitrag ich fand.

Wie auch immer, dies ist zu schwer, nicht manuell zu implementieren. Hier ist mein polymorpher Link namens Subject.

Implementieren inverser Teil der Beziehung könnte etwas komplizierter, vor allem, weil Sie denselben Code in mehreren Klassen benötigen.

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

Andere Tipps

Die Beantwortung eine alte Post, aber jemand kann es nützlich finden.

Jetzt gibt es auch eine polymorphe 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

Schienen 4 +

Hier ist, wie Sie es umsetzen Polymorphe Verbände in Mongoid für ein Comment Modell, das sowohl mit einem Post und Event Modell gehören.

Das Comment Modell:

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

  # ...
end

Post / Event Modelle:

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

  # ...
end

Mit Bedenken:

In Rails 4+, können Sie die Sorge Muster verwenden und ein neues Modul namens commentable in app/models/concerns erstellen:

module Commentable
  extend ActiveSupport::Concern

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

und include nur dieses Modul in Ihren Modellen:

class Post
  include Mongoid::Document
  include Commentable

  # ...
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top