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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:16:45+00:00 2026-06-14T03:16:45+00:00

Yes, I know how to create many to many relationships but nothing else. I

  • 0

Yes, I know how to create many to many relationships but nothing else. I did a google search and read official mongoid documentation but I didn’t anything about how to work with it. By work I mean inserting, updating, deleting, finding, counting…

For example, suppose I have 2 models (from documentation):

class Band
  include Mongoid::Document
  has_and_belongs_to_many :tags
end

class Tag
  include Mongoid::Document
  field :name, type: String
  has_and_belongs_to_many :bands
end

What should I do to make all the operations I mentioned above?

P.S. I use Sinatra and mongoid 3.

  • 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-06-14T03:16:46+00:00Added an answer on June 14, 2026 at 3:16 am

    Inserting

    From the documentation table immediately following your sample models (plus my comment).

    # Create a tag for a band
    band.tags.create(name: "electro")
    

    Finding

    Also from the same table.

    # Find a tag that belongs to that band, whose name is "electro"
    tag = band.tags.where(name: "electro") 
    

    Updating

    From the doc on persistence, modified for the band/tags example.

    # using variable tag from previous line.
    tag.update_attributes(name: "dubstep")
    

    Counting

    From the doc on querying, which also contains information useful for the other items here (also modified).

    band.tags.length
    

    Deleting

    This is from the persistence document also.

    # we will delete the tag from earlier
    tag.delete
    

    Lastly

    All the information needed to work Mongoid is in the docs, which are actually very thorough and useful. There is definitely a lot, but you can make it through it in a couple hours.

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

Sidebar

Related Questions

(Yes I know I can call Java code from Scala; but that is pointless;
Yes I know, this title isn't really helpfull but this is the exact problem.
I have the following code (Yes I know it's quite long winded, but I
I know this question is asked many times but I just want to clear
hello i know this has been asked and answered many times but i cant
I know this question may be asked many times but most of them suggests
Yes I know that it shouldn't be abused and that C# is primariy used
Ok so I start coding websites (yes I know I am almost 15 years
I´m trying to code a tooltip (Yes I know, I have my reasons to
I have an AXIS2/JAX-WS web service using a code first implementation (yes I know,

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.