Take the 2-minute tour ×

I've got a Article and a Tag model associated in a n-m relationship.

I want to be able to find tags of a given article and articles of a given tags, e.g.:

tags = Article.find(id).first.tags


articles = Tag.find(id).first.articles

My current Tag model is:

class Tag
  include Mongoid::Document

  has_and_belongs_to_many :articles

My current Article model is:

class Article
  include Mongoid::Document
  include Mongoid::Timestamps

  attr_accessor :tag_list

  before_save :assign_tags

  field :title, :type => String
  field :notes, :type => String

  has_and_belongs_to_many :tags


  def assign_tags
    if @tag_list
      self.tags = @tag_list.gsub(/\s+/,"").split(/,/).uniq.map do |name|
        Tag.find_or_create_by(:name => name.strip)

How can I implement this using mongoid? At the moment the tag model doesn't know anything about related articles.

Best regards!

share|improve this question
add comment

1 Answer

This doesn't help?


share|improve this answer
It seems that this was a bug in the latest Mongoid version. It's fixed in the master branch now. –  0x7FFFFFFF Sep 30 '11 at 9:32
ok. thanks for the update –  Bashar Abdullah Oct 3 '11 at 11:16
add comment

Your Answer


