当我这样做时,有什么方法可以防止必须在问题数据库中查询。
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
答案 0 :(得分: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