Mongoid relational Polymorphic Association

后端 未结 3 1807
南旧
南旧 2021-02-10 06:14

Does anyone know how to do a polymorphic association in Mongoid that is of the relational favor but not the embedding one.

For instance this is my Ass

3条回答
  •  攒了一身酷
    2021-02-10 07:03

    Answering to an ancient post, but someone may find it useful.

    Now there's also a polymorphic 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
    

提交回复
热议问题