我正在为“问答”论坛制作 rails 4 应用程序。我有一个模型 Micropost。我有一个模型 QuestionAnswer 将不同的微博连接为问题和答案:
Class Micropost < ActiveRecord::Base
has_many question_answers, foreign_key: "question_id"
has_many answers, through: :question_answers
has_one :reverse_question_answer, foreign_key: "answer_id", class_name: "QuestionAnswer"
has_one :question, through: :reverse_question_answer
end
Class QuestionAnswer < ActiveRecord::Base
belongs_to :question, class_name: "Micropost"
belongs_to :answer, class_name: "Micropost"
end
现在我想做一个数据库查询。我想使用两个 LEFT OUTER JOIN 来创建一个包含所有问题和答案的联合表:
Micropost.
joins('LEFT OUTER JOIN question_answers ON question_answers.question_id = microposts.id').
joins('LEFT OUTER JOIN microposts ON microposts.id = question_answers.answer_id')
我收到错误消息
SQLite3::SQLException: ambiguous column name: microposts.id SELECT "microposts".* FROM "microposts" LEFT OUTER JOIN question_answers ON question_answers.question_id = microposts.id LEFT OUTER JOIN microposts ON microposts.id = question_answers.answer_id ORDER BY created_at DESC
我在上面的第二个 join 命令中用答案替换了 micropost,但它抱怨没有这样的表。如何进行两次连接?-谢谢。