find_by_sql + Join + Rails 2

Hello

I have write a code to select data from two tables (T1,T2) but the problem is

@tables= T1.find_by_sql("select T1.*,T2.* from T1inner join T2on T1.followup_id = T2.id where followups.userid = '41' ")

THe problem all values that is returned from running only T1 variables,why? Where are T2 variables??

That's what find_by_sql does: you'll always get and instance of T1 (with some extra attributes though, can't remember if the console will show them by default. Also be careful doing what you;ve done because you could easily have overwritten the id attribute with the one from T2, which will cause havoc when you try and save the objects you've loaded. Lastly with the write associations you can probably do without the call to find_by_sql.

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

Where are T2 variables??

That's what find_by_sql does: you'll always get and instance of T1 (with some extra attributes though, can't remember if the console
will show them by default. Also be careful doing what you;ve done because you could easily have overwritten the id attribute with the one from T2, which will cause havoc when you try and save the objects you've loaded. Lastly with the write associations you can probably do
without the call to find_by_sql.

Fred

true, but I believe I'm correct in saying that if you did

"select T1.*, T2.name as t2_name ..." it would correctly load the "t2_name" as an attribute of the T1
object.

Yup that should work. You just won't see t2_name if you're messing
around in the console (except of course if you call attributes or
t2_name): the default output format in rails 2 just shows attributes
from the main table.

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

you could easily have overwritten the id attribute with the one
from

"select T1.*, T2.name as t2_name ..." it would correctly load the "t2_name" as an attribute of the T1 object.

Yup that should work. You just won't see t2_name if you're messing around in the console (except of course if you call attributes or t2_name): the default output format in rails 2 just shows attributes from the main table.

Fred

yes your are right and i am tottaly accept but when i did that it
throws

did what?

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

Yup that should work. You just won't see t2_name if you're messing around in the console (except of course if you call attributes or t2_name): the default output format in rails 2 just shows
attributes from the main table.

Fred

yes your are right and i am tottaly accept but when i did that it throws

did what?

Fred

@tables= T1.find_by_sql("select T1.*,T2.* from T1 inner join T2on T1.followup_id = T2.id where followups.userid = '41' ") puts @tables[:t2_name]

@tables is an array. You'd have to do something like @tables.first[:foo]

Fred

Mohamed Saeed wrote:

@tables= T1.find_by_sql("select T1.*,T2.* from T1 inner join T2on T1.followup_id = T2.id where followups.userid = '41' ") puts @tables[:t2_name]

and you missed out the "T2.name as t2_name";

Matthew Rudy Jacobs wrote:

Mohamed Saeed wrote:

@tables= T1.find_by_sql("select T1.*,T2.* from T1 inner join T2on T1.followup_id = T2.id where followups.userid = '41' ") puts @tables[:t2_name]

and you missed out the "T2.name as t2_name";

@tables= T1.find_by_sql("select T1.*,T2.name AS t2_name from T1 inner join T2 on T1.followup_id = T2.id where followups.userid = '41' ") puts @tables.first[:t2_name]

Thanks Fred Now it's working the problem was in @tables.first[:t2_name], also you are right console display only the attributes of the table that i used in finding "T1"

Thanks Mohamed