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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:21:04+00:00 2026-05-27T15:21:04+00:00

Mongoid ships with .push on a habtm, which sets a habtm relationship in both

  • 0

Mongoid ships with .push on a habtm, which sets a habtm relationship in both directions. Although delete will #delete an associated record, there’s no documented way to delete only a relationship that I have seen. Is there a better way of doing this?

Is there a better way of ensuring uniqueness?

has_and_belongs_to_many :following, {class_name: 'User', inverse_of: :followers, inverse_class_name: 'User'}
  has_and_belongs_to_many :followers, {class_name: 'User', inverse_of: :following, inverse_class_name: 'User'}

  def follow!(user)
    self.following.push(user) # this pushes the inverse as well
    self.following_ids.uniq!
    self.save!
    user.follower_ids.uniq!
    user.save!
  end

  def unfollow!(user)
    self.following.delete(user.id)
    self.save!
    user.followers.delete(self.id)
    user.save!
  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-27T15:21:05+00:00Added an answer on May 27, 2026 at 3:21 pm

    Following code worked fine for me (mongoid 2.3.x):

    class User
      include Mongoid::Document
    
      field :name, type: String
    
      has_and_belongs_to_many :following, class_name: 'User', inverse_of: :followers, autosave: true
      has_and_belongs_to_many :followers, class_name: 'User', inverse_of: :following
    
      def follow!(user)
        if self.id != user.id && !self.following.include?(user)
          self.following << user
        end
      end
    
      def unfollow!(user)
        self.following.delete(user)
      end
    end
    

    No inverse_class_name, no save calls, no special handling, but with exclusion of self-following.

    The reason is, that mongoid automatically uses dependent: nullify if not added to the relation statement. And with autosave: true the update of relationships get saved (and is only needed for following, because we do not alter followers directly). Without autosave option you need to add a save call in the methods, because mongoid doesn’t automatically save relationship updates (since 2.0.0.x).

    I put the if-clause as block, so you can alter it with exception handling (else raise FooException).

    The .delete(user) is okay, also mentioned in the mongoid docs: http://mongoid.org/docs/relations/referenced/n-n.html (scroll down to “DEPENDENT BEHAVIOUR”).

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

Sidebar

Related Questions

When using Mongoid referenced reletions what's the diffrence between dependent detroy and dependent delete
There are two classes: class Person include Mongoid::Document field :name embeds_many :addresses end class
I have a mongoid document which embeds other documents with a relation like this:
Mongoid queries are fast. For example if it a query which returns 300 documents
Rails 2.3.11 application Mongoid 1.9.2 (latest legacy branch) Since there's no documentation for Mongoid
MongoDB has two popular ORMs for Rails: MongoID and MongoMapper. Are there CouchDB ORM:s
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
The Mongoid documentation only gives one example of doing a wildcard search: Person.where(first_name: /^d/i)
I have my mongoid query in the model def self.get_result collection_name, hash_params, page, per_page
I have several Mongoid models that I'm running mapreduce on, and I'd like 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.