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 3403942
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:19:19+00:00 2026-05-18T05:19:19+00:00

I want to store blog and its tags as separate documents. Blog post should

  • 0

I want to store blog and its tags as separate documents.

Blog post should have tag_ids field and tag shouldn’t have blog_posts_ids field.

Mongoid provides many to many relations out of the box, but it requires both documents of many to many relation to have _ids field.

class BlogPost
  include Mongoid::Document
  field :title
  references_many :tags, :stored_as => :array, :inverse_of => :blog_posts
end

class Tag
  include Mongoid::Document
  field :name
  # I DON'T WANT TO STORE BLOG_POSTS_IDS IN TAG DOCUMENT
  references_many :blog_posts, :stored_as => :array, :inverse_of => :tags
end
  • 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-18T05:19:19+00:00Added an answer on May 18, 2026 at 5:19 am

    You can get around it with a method on Tag to fake the other side of the association

    class BlogPost
      include Mongoid::Document
      field :title
      references_many :tags, :stored_as => :array, :inverse_of => :blog_posts
    end
    
    class Tag
      include Mongoid::Document
      field :name
    
      def blog_posts
        # will match all BlogPost records where the tag_ids array contains self.id
        BlogPost.where(:tag_ids => self.id)
      end
    end
    

    Obviously, this isn’t as full-featured as :references_many , but you can similarly fake other aspects of the many-to-many relation. For example if you want the ability to assign a new blog_post to a tag you can add a simple create_blog_post method to Tag.

    For many real-world situations, this kind of approach is practical as long as you keep the methods simple and don’t get carried away.

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

Sidebar

Related Questions

I want to store a blog post in a database. I thought it would
i really want to add multiple tags to a blog post when a user
I want to store the TF-IDF matrix so I don't have to recalculate it
I have an idea for my wordpress blog. I want to have a page
I have an Umbraco based blog and I want to display the top 10
I have a post widget in a blog application which are stored in a
I want to create password protected model. For example Post on the blog. I
I've read in a number of places (ie. http://iphoneincubator.com/blog/app-store/rejections ) that you should not
I have a self-hosted WordPress blog post that got popular and has over 100
i have a wordpress blog hosted over my personal shared hosting.Now i want to

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.