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,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

-- 
Posted via http://www.ruby-forum.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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to