rubymongodbruby-on-rails-4ruby-on-rails-5mongoid4

Implementing has_many_through & polymorphic association in Mongoid


I have 4 models, User(Postgres), Subscription(Mongo), Podcast (Mongo), and Newspaper (Mongo). A user can have multiple podcasts through subscriptions and a podcast can have multiple users through subscription. A subscription also has fees field with it. How can I implement has_many_through & polymorphic association in this scenario? Stub:

class User < ActiveRecord::Base
end  


 class Podcast
  include Mongoid::Document
 end



class Newspaper
  include Mongoid::Document
 end



class Subscription
  include Mongoid::Document
  field :fees
 end

Thanks in advance! :)


Solution

  • I forgot to include Active Record Bridge. Basically, it is used for linking AR models with Mongo's Documents.

    include Mongoid::ActiveRecordBridge