1

I am creating a instance variable that gets passed to my view. This variable 'post' has a user_id associated with it and I wanted to add an extra attribute called 'username' so I can also pass that and use it in the view.

Here is an example of what I would like to do.

@post = Post.find(params[:id])
@post.username = User.find(@post.user_id).username

A username column does exist on my Users model but not my Songs model. So it won't let me use

@post.username

I know I can just make an entirely new instance variable and put that information in there but I would like to keep everything nice and neat, in one variable. Which will also make my json rendered code look cleaner.

Any ideas on how I can accomplish this?

Thanks!

2 Answers 2

4

Based on the presence of a user_id in your Post model, you probably already have an association set up that can retrieve the username. It will probably save a lot of trouble to simply use the existing association:

@post = Post.find(params[:id])
username = @post.user.username

If you're likely to be querying more than one post at a time (e.g., on an index page, calling .includes to tell Rails to eager-load an association will help you avoid the N+1 problem:

@posts = Post.includes(:user).all

Finally, to include the associated record in your JSON output, pass the :include parameter as you serialize:

# in controller
render :json => @post.to_json(:include => :user)

This question includes a much more comprehensive discussion of serialization options. Well worth a read.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, I had no idea about this association. Is there anyway to add the username information to the json output for the page?
That's a separate issue, how are you rendering it to JSON? If you are using the defaults then pass options into the to_json call. E.g. render :json => @post.to_json(:include => :user). Or if you want the Post JSON object to have the username everywhere in your app then customize the as_json method in the Post model.
awesome thanks! that's exactly what I needed. I'm using the @post.user.username association in my views and added the json code to get the user in my controller. Thanks again for the quick replies.
2

No need to pass a separate instance variable. 1. You can use @post.user.username in view itself. 2. Or you can create a helper and pass @post.user

def username user
 user.username
end

3 Comments

Ya @post.user.username worked just fine. However my json output still has user_id = # is there anyway I can also add the username to the json output for the page?
You need to add user while rendering json as @post.to_json(:include => :user)
thanks asitmoharna this solution worked. thanks for the quick reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.