Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally include associations in JSON response #141

Open
kapso opened this issue Jul 30, 2023 · 3 comments
Open

Conditionally include associations in JSON response #141

kapso opened this issue Jul 30, 2023 · 3 comments

Comments

@kapso
Copy link

kapso commented Jul 30, 2023

Can I do something like this - ability to pass params or context to a serializer?

class UserSerializer < Panko::Serializer
  attributes :id, :name, :age

  # This will only include posts when if condition is true
  has_many :posts, if: -> { params[include_posts: true] }
end

UserSerializer.new(params: { include_posts: true })
@yosiat
Copy link
Owner

yosiat commented Jul 30, 2023

Hey,

You have two options to achieve this:

  1. Via Nested Filters
  2. In serializer with combination of passing "include_posts" as context and Filters For

If the name of the parameter you plan to pass is indicating which attributes/associations you want to serializer (like include_name / include_posts), then nested filters will fit you better. But, if your parameter is for example current logged in or other contextual information, which based on that you want to selectively serialize fields than context and filters for will be the best match here.

Does it makes sense?

@kapso
Copy link
Author

kapso commented Jul 31, 2023

@yosiat I can use filters, which will prevent that data from being returned. But I am also trying to prevent posts query to be made, maybe coz its expensive.

@yosiat
Copy link
Owner

yosiat commented Oct 23, 2023

@kapso when you use filters to filter out posts it shouldn't run the posts query (since posts won't be accessed)

Here is an example:

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "~> 7.1.0"
  gem "sqlite3"
  gem "pg"

  gem "panko_serializer"
end

require "active_record"
require "logger"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'rails_pg_guide')

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.integer :user_id
    t.text :context
  end

  create_table :users, force: true do |t|
    t.integer :age
  end
end

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user
end


class PostSerializer < Panko::Serializer
  attributes :content
end


class UserSerializer < Panko::Serializer
  attributes :id, :name, :age
  has_many :posts, serializer: PostSerializer
end

# Create user with some data
user = User.create!(age: 18)
10.times { user.posts << Post.create!(user: user) }

# Use serializer
#  note: I am reloading the user, so it will re-issue the queries.
ActiveRecord::Base.logger = Logger.new(STDOUT)
pp UserSerializer.new(except: [:posts]).serialize(user.reload)

With this example, there is no query for posts.
If I am missing something, please fix my example to show what you are seeing 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants