Comments (3)
Hey,
You have two options to achieve this:
- Via Nested Filters
- 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.
@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.
@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)
- Delegate + aliases not working as expected HOT 1
- Adding a custom root name for array serializer HOT 1
- Relax `oj` dependency HOT 3
- Is anyone actively maintaining this project? HOT 2
- Using ActiveRecord's pluck when possible
- Serialize attributes which is used `serialize` in active model definition
- Benchmarks task not working HOT 1
- How do I selectively display a key/value pair? HOT 2
- Alias declaration is ignored when method is added HOT 2
- Migration from AMS HOT 3
- Filter on array serializer? HOT 4
- Rendering recursive relations HOT 2
- Add context to relations has_one/has_many
- Ability to skip nil values HOT 2
- Loading dynamic field names during serialization HOT 1
- Documentation questions HOT 3
- Status HOT 1
- Serialization of single object with context / options HOT 2
- How to use a raw JSON string inside a serializer? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from panko_serializer.