On 17 October 2010 13:04, Rahul J. <[email protected]> wrote: > 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 > Thanks in advance. > > -- > 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. > > -- 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.

