2012-08-30 76 views
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")]}) 

回答

1

如果我這樣做了之後,那麼它會正常工作:

has_and_belongs_to_many :tags, autosave: true # Refer to tag_ids 
相關問題