I have 3 tables want to join.
questions: id, question_category_id, content
answers: id, question_id, created_at, content, person_id
question_categories: id, name, created_at
and now I do this on rails 2.0.2:
@myanswers = Answer.find_by_sql("
SELECT answers.*, question_categories.name AS
question_category_name
FROM answers
JOIN questions on answers.question_id=questions.id
JOIN question_categories on questions.question_category_id =
question_categories.id
WHERE answers.person_id = 12
ORDER BY answers.created_at DESC
")
puts @myanswers
I just got [#<Answer id: 2, question_id: 3, created_at: “2008-01-15
16:11:12”, content:“this is test”, person_id: 12 > but there is no
question_category_name. when I run this in MYSQL I can got:
id: 2, question_id: 3, created_at: “2008-01-15 16:11:12”,
content:“this is test”, person_id: 12, question_category_name:
“Movies / TV”
why did happen?
by the way, When I run this on rails 1.2.6. there is no problem. and
the result is my wanted. like:
id: 2, question_id: 3, created_at: “2008-01-15 16:11:12”,
content:“this is test”, person_id: 12, question_category_name:
“Movies / TV”
who can help me? Thanks!!