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. "
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001377
On Jan 15, 2008 10:21 PM, OnRails
<zzzlai-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org>
wrote:>
> 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!!
> >
>
--
Rafael Mueller
http://queroseragil.wordpress.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---