Coder Social home page Coder Social logo

Comments (3)

yosiat avatar yosiat commented on June 13, 2024

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?

from panko_serializer.

kapso avatar kapso commented on June 13, 2024

@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.

from panko_serializer.

yosiat avatar yosiat commented on June 13, 2024

@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 🙏

from panko_serializer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.