# Mysql is wrong or rails 2.0.2 is wrong?

**URL:** <https://www.ruby-forum.com/t/mysql-is-wrong-or-rails-2-0-2-is-wrong/126177>\
**Category:** Rails\
**Created:** [January 16, 2008, 2:22am UTC](https://www.ruby-forum.com/t/mysql-is-wrong-or-rails-2-0-2-is-wrong/126177 "2008-01-16T02:22:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [January 16, 2008, 2:22am UTC](https://www.ruby-forum.com/t/mysql-is-wrong-or-rails-2-0-2-is-wrong/126177/1 "2008-01-16T02:22:20Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [January 16, 2008, 2:52am UTC](https://www.ruby-forum.com/t/mysql-is-wrong-or-rails-2-0-2-is-wrong/126177/2 "2008-01-16T02:52:09Z")

</div>

I think this can help you

"find\_by\_sql(sql)

Executes a custom sql query against your database and returns all the  
results. The results will be returned as an array with columns  
requested encapsulated as attributes of the model you call this method  
from. If you call +Product.find\_by\_sql+ then the results will be  
returned in a Product object with the attributes you specified in the  
SQL query. "

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001377)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

On Jan 15, 2008 10:21 PM, OnRails [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> question\_category\_name  
> question\_category\_name. when I run this in MYSQL I can got:
> 
> who can help me? Thanks!!
> 
> >

–  
Rafael Mueller

> **[Quero ser Ágil](https://queroseragil.wordpress.com)**
>
> Idéias e experiências de um desenvolvedor em busca da Agilidade
