2
我有一個標籤和一個問題類。我如何級聯保存與mongoid的has_and_belongs_to_many關係?
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
然而,當我做了以下標籤不保存:
t = Tag.new(id: "politics")
q = Question.new({tags: [t]})
q.save!
然而,當我這樣做是創建的標籤:
Question.create!({tags: [Tag.new(id: "politics")]})