2

I have a Tag and a Question class.

class Question
  include Mongoid::Document
  include Mongoid::Timestamps
  identity type: String
  has_and_belongs_to_many :tags # Refer to tag_ids
end

class Tag
  include Mongoid::Document
  identity type: String    
  has_and_belongs_to_many :questions
end

However the tags do not save when I do the following:

t = Tag.new(id: "politics")
q = Question.new({tags: [t]})
q.save!

However, the tags are created when i do this:

Question.create!({tags: [Tag.new(id: "politics")]})
Kamilski81
  • 14,409
  • 33
  • 108
  • 161

1 Answers1

1

If i do the following, then it will work correctly:

has_and_belongs_to_many :tags, autosave: true # Refer to tag_ids
Kamilski81
  • 14,409
  • 33
  • 108
  • 161