2017-02-28 26 views
0

兩次我有兩個類Intern::QuestionIntern::Answer,標準協會的樣子:同事同型號的mongoid

class Intern::Question 
    has_many :intern_answers, class_name: 'Intern::Answer' 
end 

class Intern::Answer 
    belongs_to :intern_question, class_name: 'Intern::Question' 
end 

,現在我想引用兩次回答belongs_to的問題,答案可以存儲父母的問題和未來問題是這樣的:

class Intern::Question 
    has_many :intern_answers, class_name: 'Intern::Answer' 
    has_many :node_for_answers, class_name: 'Intern::Answer' 
end 

class Intern::Answer 
    belongs_to :intern_question, foreign_key: :intern_question_id, class_name: 'Intern::Question' 
    belongs_to :next_question, foreign_key: :next_question_id, class_name: 'Intern::Question' 
end 

但我不得不嘗試,並獲得此錯誤:

Mongoid::Errors::AmbiguousRelationship 
+0

@muistooshort抱歉錯字.. – itx

回答

0

實測溶液here,使用inverse_of

class Intern::Question 
    has_many :intern_answers, class_name: 'Intern::Answer', inverse_of: :intern_question 
    has_many :node_for_answers, class_name: 'Intern::Answer', inverse_of: :next_question 
end 

class Intern::Answer 
    belongs_to :intern_question, foreign_key: :intern_question_id, class_name: 'Intern::Question', inverse_of: :intern_answers 
    belongs_to :next_question, foreign_key: :next_question_id, class_name: 'Intern::Question', inverse_of: :node_for_answers 
end