Today I Learned

hashrocket A Hashrocket project

Filter Fields and Associations When Rendering JSON

Today I learned you can modify a json payload in a rails controller to include associated models and filter attributes in the payload with the include and only options.

Say I have a controller that returns all blog Post records as json:

def index
  respond_to do |format|
    format.json do
      render json: Post.all
    end
  end
end

Now, say I want to include attributes for an associated Author model, but only their first and last name and exclude all other attributes. I can include the authors, and only include first and last name:

def index
  respond_to do |format|
    format.json do
      render json: Post.all, include: {author: {only: [:last_name, :first_name]}}
    end
  end
end
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.