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

On Jun 26, 10:08 pm, Mohamed S. [email protected]
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

Frederick C. wrote:

On Jun 26, 10:08�pm, Mohamed S. [email protected]
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.

On 27 Jun 2008, at 09:40, Matthew R. Jacobs 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

Frederick C. wrote:

On 27 Jun 2008, at 09:40, Matthew R. Jacobs 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
exeption “Attribute doesn’t exist in array…”

On 27 Jun 2008, at 10:39, Mohamed S. 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

Frederick C. wrote:

On 27 Jun 2008, at 10:39, Mohamed S. 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]

On 27 Jun 2008, at 10:58, Mohamed S. wrote:

Fred
puts @tables[:t2_name]
@tables is an array. You’d have to do something like @tables.first[:foo]

Fred

Matthew R. Jacobs wrote:

Mohamed S. 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

Mohamed S. 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]