2012-09-19 24 views
1

當我這樣做時,有什麼辦法可以防止查詢db的問題。如何插入使用mongoid,而不必做額外的查詢?

has_and_belongs_to_many :followed_questions, class_name: 'Question', inverse_of: nil 
qid = "501928374" 
q = Question.find(qid) 
self.followed_questions << q unless self.followed_questions.include?(q) 
self.save 

我想這樣做:

has_and_belongs_to_many :followed_questions, class_name: 'Question', inverse_of: nil 
qid = "501928374"  
self.followed_questions << qid unless self.followed_questions.include?(qid) 
self.save 

回答

1

是的,你可以通過指定id,而不是文件

has_and_belongs_to_many :followed_questions, class_name: 'Question', inverse_of: nil 
qid = "501928374"  
self.followed_question_ids << qid unless self.followed_question_ids.include?(qid) 
self.save