activerecord variable apart from member

Hi,

I have a table with a field of user_id. In find , i had created a join
on user table to retreive the username as:

@question = Question.find(params[:id], :select=>"questions.*,
users.username as username",:joins=>" inner join users on users.id =
questions.user_id");

I had created an instance variable in Question class with name
"username". But i am not able to access the data. I want to access it as
question.username. can anyone help me out.

Also i am not preffering associations to get the complete user field, as
its not being needed.
Thanks in advance.

Hi,

I have a table with a field of user_id. In find , i had created a join
on user table to retreive the username as:

@question = Question.find(params[:id], :select=>"questions.*,
users.username as username",:joins=>" inner join users on users.id =
questions.user_id");

I had created an instance variable in Question class with name
"username". But i am not able to access the data. I want to access it as
question.username. can anyone help me out.

Also i am not preffering associations to get the complete user field, as
its not being needed.

Why do you not want to use associations? I am sure it is the right
way to do it. Say Question belongs_to user and then you can use
question.user.username or if you define a method on question that
returns the username (with checks for nil user id) then you can say
question.username.

If you are worried about efficiency then don't worry. It is most
unlikely that something like this will have any impact on your app and
if it eventually does then that is the time to optimise it. After
all, how many fields does User have that you are saving time by not
fetching?

Colin

@question = Question.find(params[:id], :select=>"questions.*,
users.username as username",:joins=>" inner join users on users.id =
questions.user_id");

I had created an instance variable in Question class with name
"username". But i am not able to access the data. I want to access it as
question.username. can anyone help me out.

you shouldn't need to do anything - AR will create an attribute reader
method for you

Fred

Hi Colin,
I don't prefer associations here, coz i'll be having a large list of
questions approx 50) and associated answers(approx. 500), so i don't
want to fetch users record along with all those.
@Fred : I am not getting that field as "username" is not in question Ar,
but its in Users.

How can i specify the fields that are not in model but are retrieved
through sql to be stored in some instance variables.

Thanks,
Rahul

Hi Colin,
I don't prefer associations here, coz i'll be having a large list of
questions approx 50) and associated answers(approx. 500), so i don't
want to fetch users record along with all those.
@Fred : I am not getting that field as "username" is not in question Ar,
but its in Users.

How can i specify the fields that are not in model but are retrieved
through sql to be stored in some instance variables.

You don't need to do anything - anything in the select clause is
available. It doesn't show up in the output of inspect (which is what
you see if you're playing around in the console) but you can still
call @question.username.

Fred

Why not?
Note that it will only fetch the user record if you access it. Also
50 is a very small number of records, fetching the user record along
with each of those is trivial.

Colin

Rahul J. wrote in post #954924:

Hi,

I have a table with a field of user_id. In find , i had created a join
on user table to retreive the username as:

@question = Question.find(params[:id], :select=>"questions.*,
users.username as username",:joins=>" inner join users on users.id =
questions.user_id");

I had created an instance variable in Question class with name
"username". But i am not able to access the data. I want to access it as
question.username. can anyone help me out.

Question belongs_to :user

question.user.name

Done!

Also i am not preffering associations to get the complete user field, as
its not being needed.

Then you are most likely being stupid. The proper way to do this is
with associations. You don't have to run the joins when you don't need
the user data.

Thanks in advance.

Best,

I think that is a bit harsh Marnen, one can be misguided through lack
of knowledge without being stupid.

Colin

Colin Law wrote in post #955070:

Rahul J. wrote in post #954924:

[...]
Also i am not preffering associations to get the complete user field, as
its not being needed.

Then you are most likely being stupid.

I think that is a bit harsh Marnen, one can be misguided through lack
of knowledge without being stupid.

You're right, it was harsh, and if it was too much so then I apologize.
I wouldn't have been that harsh except that the OP repeatedly said that
he didn't want to do this with associations -- that is, that he didn't
want to do it right -- and the reasons he gave were such that I believe
he didn't understand how associations worked and was rejecting them for
the wrong reasons. I chose strong language to attempt to snap him out
of his stubbornness. Whether it was the right choice or not is another
question. :slight_smile:

Colin

Best,

Hi Colin,
I don't mind your comment, but appreciate you for explaining me.
Actually, its the first time i am using scripting language for my
purpose and i am use to of having only needful info. In terms of
complexity, there is no difference, as in both of cases (manual join or
associations), things will be same. But only thing that i was refusing
is fetching whole 500 users records, while i am needing only one field
of that record.
Generally a programming language is supposed to provide platform over
which you can build your functionality. But scripting language is
providing a part of the functionality and you have to build your own
over that, instead you doing everything from scratch.
After learning ruby & rails more and more, i found that the best is to
go with the defaults/functionality provided by the language.

Thanks,
Rahul

Hi Colin,
I don't mind your comment, but appreciate you for explaining me.

Which comment? You have not quoted the email so I have to guess what
you are talking about.

Actually, its the first time i am using scripting language for my
purpose and i am use to of having only needful info. In terms of
complexity, there is no difference, as in both of cases (manual join or
associations), things will be same. But only thing that i was refusing
is fetching whole 500 users records, while i am needing only one field
of that record.

Do you need one field from all 500 records? If so then the overhead
of fetching all fields is trivial. If you mean that you only need
data from a few of the records then this is not a problem, the
individual user records will only be fetched if you access
them.

Generally a programming language is supposed to provide platform over
which you can build your functionality. But scripting language is
providing a part of the functionality and you have to build your own
over that, instead you doing everything from scratch.
After learning ruby & rails more and more, i found that the best is to
go with the defaults/functionality provided by the language.

Definitely, Rails is all about providing easy ways of doing things,
but to make use of those you must accept the Rails way of doing
things. It is possible to do things other ways but it may not be easy
and there is little point in using Rails if you do not do things the
Rails way. So in this case use associations and accept the trivial
overheads. Later on if it becomes clear that the overheads are
slowing your application down _then_ is the time to optimise the code.

Colin