Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:19:04+00:00 2026-05-31T13:19:04+00:00

I am creating a instance variable that gets passed to my view. This variable

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:19:05+00:00Added an answer on May 31, 2026 at 1:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a property without creating an instance variable. Is it possible
While creating classes in Java I often find myself creating instance-level collections that I
I´m dynamically creating an instance of a class with reflection and this works fine,
My Dispatcher is choosing correct Controller; then creating Controller's instance (DependencyInjectionContainer is passed to
I have some code using a System.Transactions.TransactionScope , that creating a new instance of
After creating a instance of a class, can we invoke the constructor explicitly? For
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
How do you go about creating an instance of an object when given the
Having a bit of a problem creating an instance of an object. Bear in
What's the correct way to prevent invoking (creating an instance of) a C type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.