Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6154433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:15:45+00:00 2026-05-23T20:15:45+00:00

In Mongoid how would I achieve the same thing that ActiveRecord :through => provides?

  • 0

In Mongoid how would I achieve the same thing that ActiveRecord :through => provides?

class Advertiser
    include Mongoid::Document
    embeds_many :campaigns

    # how would I do this
    embeds_many :advertisements, :through => :campaigns

end

class Campaign
    embedded_in :advertiser
    embeds_many :advertisements
end

class Advertisement
    embedded_in :campaign

    # or this?
    embedded_in :advertiser, :through => :campaign
end

then be able to do
Advertiser.first.advertisements
and
Advertisement.first.advertiser

Advertiser.campaigns.collect{|campaign| campaign.advertisement} is not an option

How, how would I do these with references_many / referenced_in?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:15:46+00:00Added an answer on May 23, 2026 at 8:15 pm

    The short answer is that you can not. MongoDB does not have the concept of a join table nor does it do joins in general. The Mongoid many-to-many “simulation” is done by storing arrays of foreign keys on each side.

    In response to a comment:
    MongoDB is a document store. It therefore suits situations where “documents” are highly heterogeneous. As you store your Advertisers with a subtree of Campains and Advertisements, you will have to collect the Advertisements for the Advertisers in ruby code. If your data has a very homogeneous form then you could consider using a relational database instead. We often used MySQL for relating objects and then added MongoDB documents to the objects so they would be extensible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Parent include Mongoid::Document embeds_many :children field :title end class Child include Mongoid::Document embedded_in
I have a model like this: class Search include Mongoid::Document embeds_many :terms accepts_nested_attributes_for :terms
I have a class that looks like this: class SearchService include Mongoid::Document key :name,
If I have a model... class Post include Mongoid::Document field :link field :title field
Let’s say you have the following mongoid documents: class User include Mongoid::Document embeds_one :name
I've got notification embedded in user. In Mongoid, how would I achieve a similar
How would you go about deep cloning a document in MongoDB (mongoid) I've tried
I'm learning MongoDB through the Mongoid Ruby gem with Rails (Rails 3 beta 3),
Is there an easy way to get all attributes of a Mongoid document, including
I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's to_json method,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.