views:

18

answers:

1

I have a table with a user_id field. In find, I 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 created an instance variable in Question class with name username. But I'm not able to access the data. I want to access it as question.username. can anyone help me out, how to do that?

Also I'm not using associations to get the complete user field, because not needed. Thanks in advance.

A: 

As said before, when you use "users.username as username", Rails creates a "username" method that should return the info. If you create another username instance variable, now Ruby won't be sure of which one to use when you typed "question.username" and will err on the side of using the instance variable (which doesn't have a value).

Try removing the instance variable declaration and it should work :)

Yaraher