4

in my controller I currently have:

invite = Invite.find_by_token(params[:id])
user = invite.user

json_response({
  user: user
})

def json_response(object, status = :ok)
  render json: object, status: status
end

Right now, user is returning all user fields. I want to return just (id, email)... I've tried:

user = invite.user.select(:id, :email)
user = invite.user.pluck(:id, :email)

neither works. Ideas?

4
  • 1
    I don't think this is an accurate representation of your issue. render json: {user: user.pluck(:id, :email), status: status} will not return the whole serialized user object. If you have simplified the problem in an attempt to provide a minimal scenario, can you please ensure it's still a verifiable description? Commented Jun 12, 2017 at 15:51
  • @TomLord your suggestion results in an error undefined method pluck' for #<User` Commented Jun 12, 2017 at 15:52
  • 1
    Ah right sorry, I forgot we were dealing with a single object here .... So you could have done user.attributes.extract!('id', 'name'). Using as_json is better though. My point, basically, was that your question did not describe your error. Commented Jun 12, 2017 at 16:00
  • @TomLord sorry about that.. Commented Jun 12, 2017 at 16:01

2 Answers 2

6

You can use the method as_json passing attributes you want in the response, like:

user.as_json(only: [:id, :email])

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

Comments

4

I know this question already has an answer, but there is also a nice gem you could use called active_model_serializers. This lets you specify exactly which properties you want in your JSON output for different models and even let's you include relationships to other models in your response.

Gemfile:

gem 'active_model_serializers', '~> 0.10.0'

Then run bundle install.

You can then create a serializer using the generator command:

rails g serializer user

which will create the serializer in project-root/app/serializers/.

In your serializer, you can whitelist the attributes you would like:

project-root/app/serializers/user_serializer.rb:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :email
end

Now any time you return a User object it will only output those two attributes, id and email.

Want to print out related models? Easy. You can just add the relationship in your serializer and it will include those related models in your JSON output.

Pretend a user "has many" posts:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :email

  has_many :posts
end

Now your JSON outputs should look something like:

{
  "id": 1,
  "email": "[email protected]",
  "posts": [{
    id: 1,
    title: "My First Post",
    body: "This is the post body.",
    created_at: "2017-05-18T20:03:14.955Z",
    updated_at: "2017-05-18T20:03:14.955Z"
  }, {
    id: 2,
    title: "My Second Post",
    body: "This is the post body again.",
    created_at: "2017-05-19T20:03:14.955Z",
    updated_at: "2017-05-19T20:03:14.955Z"
  }, 
  ...
  ]
}

Pretty neat and convenient. And if you want to limit the the posts to only print certain columns as well, all you need to do is create a serializer for posts, specify the attributes, and the output will just work.

Comments

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.