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 7176823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:35:12+00:00 2026-05-28T16:35:12+00:00

I have an object in Rails that has attributes A, B, C, D, and

  • 0

I have an object in Rails that has attributes A, B, C, D, and E. When passing this object back to the client-side through a JSON object, how can I tell the rails controller to only include attributes A and D in the JSON object?

Within my Users controller, my code is as follows:

    @user = User.find(params[:id])

    respond_to do |format|
        format.html
        format.json { render :json => @user}
    end

This code works, however, the JSON object that is returned contains all the attributes of the @user object. How can I limit the attributes that are included in the JSON object before anything is sent back to the client?

UPDATE: lucapette provides some good background about what’s happening behind the scenes. Since there are times when I’d probably want all attributes returned, I ended up using the following code:

    format.json { render :json => @user.to_json(:only => ["id"])}
  • 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-28T16:35:13+00:00Added an answer on May 28, 2026 at 4:35 pm
    render :json => @user
    

    will call to_json on the @user object. And the to_json method will use the as_json method to do its work. So you can easily override the as_json to pass only what you want to the clients. Like in the following:

    def as_json options={}
      {
        attr1: attr1,
        attr2: attr2
      }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Rails app that instantiates a SWF object 16 times (it has
I have two tables: object that has object_id column and avalues that have object_id
I have the following object that has been created @post = Post.create(:name => 'test',
I have a Ruby on Rails app that has a scheduled job I execute
I have an object JobBreakdown that has_one :invoice . If a JobBreakdown has an
In rails 3, I have a database model that has a birthday field, of
How can I generate form fields for a has_many :through association that has additional
I have a rails template (.rhtml file) generating a Javascript object. It looks something
I have object A which in turn has a property of type Object B
I think I read somewhere that some modules only have object oriented interfaces (

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.