1

有什么方法可以防止我在执行此操作时不必向数据库查询问题。

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
4

1 回答 1

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
于 2012-09-20T10:06:18.813 回答